【发布时间】:2013-02-27 08:02:32
【问题描述】:
我正在制作一些室内和室外活动的相当复杂的情节,但我有点卡住了。我想在我的情节的geom_segment 步骤中添加(Xmin、Ymin 和Zmin)在室内/室外花费的分钟数(见下文)。它目前由 only Zmin 着色(使用连续变量,这有点不对劲)。
我已经粘贴了所有代码以防其他人想要构建这样的东西,我最初是受到this blog post 的启发。
任何帮助将不胜感激,谢谢!
df <- data.frame(
date = seq(Sys.Date(), len= 156, by="4 day")[sample(156, 26)],
action = paste('Then', LETTERS[1:13], 'happed, which was not related to', LETTERS[14:26]),
IndoorOutdoor = rep(c(-1,1), 13),
Xmin = sample(90, 26, replace = T),
Ymin = sample(90, 26, replace = T),
Zmin = sample(90, 26, replace = T)
)
df$XYZmin <- rowSums(df[,c("Xmin", "Ymin", "Zmin")])*df$IndoorOutdoor
# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)
# step 1
plot <- ggplot(df,aes(x=date,y=0))
# step 2, this is where I want to add 'more' color
plot <- plot + geom_segment(aes(y=0,yend=XYZmin,xend=date, colour= Zmin))
# The text is added as follows
plot <- plot + geom_text(aes(y=XYZmin,label=paste(action, '\n this happed on ', date)),size=2.5,hjust=-.01, vjust=-.01, angle = 35)
# points at the end of the line segments
plot <- plot + geom_point(aes(y=XYZmin))
# #raw a vertical line
plot <- plot + geom_hline(y=0,size=1,color='purple')
#drawing the actual arrow
# plot <- plot + geom_segment(x=2011.4,xend=2012.2,y=.2,yend=0,color='purple',size=1) + geom_segment(x=2011.4,xend=2012.2,y=-.2,yend=0,color='purple',size=1)
plot <- plot + theme(axis.text.y = element_blank()) + ylab('') + xlab('')
plot + labs(title = "Timeline for when what happened")
【问题讨论】:
-
Zmin映射,作为一个连续变量,以可定义的方式着色。您希望如何将三个数字映射到一个色标?如果它们是离散的,我可以看到它们之间相互作用的映射,但这对连续变量没有意义。 -
@BrianDiggs,感谢您回答我的问题。是我的表述不准确。我设想
Xmin、Ymin和Zmin的离散版本,并通过三种离散颜色对各个段进行着色。这有意义吗? -
@BrianDiggs,喜欢these segment 或喜欢this line。如果这回答了您的问题,请告诉我。如果不是,我很乐意尝试在我的情节中说明它。
标签: r time plot ggplot2 visualization