2013-03-11 209 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