2013-03-11 210 views
4

我正在处理一些相当精细的一些室内和室外活动情节,而我有点卡住了。我想在我的情节的geom_segment步骤中添加(Xmin,YminZmin)在室内/室外花费的分钟数(参见下文)。目前仅有着色Zmin(使用一个连续变量,这是有点关闭)。ggplot2,geom_segment的颜色段

我粘贴了所有的代码,以防其他人想要构建类似的东西,我最初的启发是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") 

enter image description here

+0

“Zmin”映射作为一个连续变量以可定义的方式着色。你想如何将三个数字映射到单一色阶?如果它们是离散的,我可以看到一个映射到它们之间的相互作用,但是这对连续变量没有意义。 – 2013-03-11 23:32:46

+0

@BrianDiggs,谢谢你回复我的问题。这是我的表述不准确。我设想了一个独立版本的'Xmin','Ymin'和'Zmin',并用三种不同的颜色将各个部分着色。那有意义吗? – 2013-03-11 23:37:57

+0

@BrianDiggs,像[这些细分市场](http://i.stack.imgur.com/78BKj.png)或像[此行](http://3.bp.blogspot.com/-N7UsdR5vws8/T9Ezi4MQU4I/ AAAAAAAAAA8/oNq1iNZtq0E/S1600/lineWithDifferentColorsAndSize.JPG)。请让我知道这是否回答你的问题。如果不是,我很乐意尝试在我的情节中说明它。 – 2013-03-11 23:41:18

回答

9

这里有两种方法。一个构造出不同geom_segment的每一行(我认为这更接近你在做什么?)另一个使用geom_bar,这将是我的偏好(更可扩展)。我认为在制作data.frame时出现错误 - 当您制作IndoorOutdoor时会创建56行,这会在稍后产生问题。我改变了下面的内容。

set.seed(1234) 
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), #Change so there are only 26 rows 
    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 
df[,8:10] <- df[,4:6]*df[,3] #Adding the sign to each X/Y/Z 
names(df)[8:10] <- paste0(names(df)[4:6],"p") #to differentiate from your X/Y/Z 
require(ggplot2) 

对于geom_bar溶液然后我熔融df使得单个值可以被绘制,并且fill映射到varialbe(Xmin等)

df.m <- melt(df[,c(1:3,7:10)], measure.vars=c("Xminp", "Yminp", "Zminp")) 
plot.alt <- ggplot(df.m, aes(date, value, fill=variable)) + 
    geom_bar(position="stack", stat="identity", width=0.5) + #width is just to make it look like yours 
#All of this is from your original plot 
    geom_text(aes(y=XYZmin,label=paste(action, '\n this happed on ', date)),size=2.5,hjust=-.01, vjust=-.01, angle = 35) + 

    geom_point(aes(y=XYZmin), show_guide=FALSE) + 
    geom_hline(y=0,size=1,color='purple') + 
    theme(axis.text.y = element_blank()) + ylab('') + xlab('') + 
    labs(title = "Timeline for when what happened") 
plot.alt 

enter image description here

可替代地,这里是使用geom_segment建立它的方法:

plot <- ggplot(df,aes(x=date)) + 
#Three geom_segments 
    geom_segment(aes(y=0,yend=Xminp,xend=date), colour= "blue") + 
    geom_segment(aes(y=Xminp,yend=Xminp + Yminp,xend=date), colour= "red") + 
    geom_segment(aes(y=Xminp + Yminp,yend=Xminp + Yminp + Zminp,xend=date), colour= "green") + 
#This is all from your plot 
    geom_text(aes(y=XYZmin,label=paste(action, '\n this happed on ', date)),size=2.5,hjust=-.01, vjust=-.01, angle = 35) + 
    geom_point(aes(y=XYZmin)) + 
    geom_hline(y=0,size=1,color='purple') + 
    theme(axis.text.y = element_blank()) + ylab('') + xlab('') + 
    labs(title = "Timeline for when what happened") 
plot 

希望有道理......让我知道如果它不是你以后的样子。 enter image description here

+0

谢谢你写得很好的答案! – 2013-03-12 04:15:28