2013-11-05 27 views
1

我的問題與this one類似,但我無法爲我的情節設定該答案。ggplot2中的每個面都只能顯示一個子集

我在使用geom_linerange製作一組名稱(1:20),每個名稱都與贊助商(B:E)相關聯。我想「分面」圖表,所以名稱/時間線由贊助商分組。到目前爲止,如果我創建一個包含贊助商+名稱的「組合」因素,我可以得到一個合併的情節。但是,如果我嘗試着面,那麼每個贊助商都會獲得全部的名稱。

這裏是我的我的數據集(修改集)(我使用lubridate的日期......):

structure(list(Sponsor = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("B", 
"C", "D", "E"), class = "factor"), Last = structure(1:20, .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20"), class = "factor"), 
Grant = c(0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.5, 
0, 1, 1, 0), Start = structure(c(844128000, 904003200, 1001289600, 
1314835200, 1064188800, 1107561600, 1138838400, 1264896000, 
1222819200, 1042502400, 1343779200, 904521600, 950832000, 
1009929600, 1081209600, 1171929600, 821664000, 865209600, 
979603200, 1209600000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
End = structure(c(929232000, 946684800, 1088035200, NA, 1109721600, 
1324512000, 1232496000, NA, NA, 1101859200, 1388448000, 993859200, 
1006819200, 1103500800, 1139529600, 1235952000, 919036800, 
1030665600, 1047254400, 1272585600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Combo = c("B_1", "B_2", "B_3", 
"B_4", "B_5", "B_6", "B_7", "B_8", "C_9", "C_10", "C_11", 
"D_12", "D_13", "D_14", "D_15", "D_16", "E_17", "E_18", "E_19", 
"E_20")), .Names = c("Sponsor", "Last", "Grant", "Start", 
"End", "Combo"), row.names = c(NA, 20L), class = "data.frame") 

這裏是命令生成非方位的曲線圖,組東西按照順序排列,但沒有細分:

library(ggplot2) 
require(lubridate) 

YearLine = ymd(19960101) + years(seq(0,18)) 

ggplot(testdat,aes(Combo, Start, ymin=Start,ymax=End,color=as.factor(Grant),xticks)) + xlab("Sponsor") + geom_linerange(size=4,alpha=.7) + geom_point(size=4,shape=18) + coord_flip() + scale_colour_brewer(palette="Spectral") + scale_x_discrete(labels=testdat$Sponsor) + geom_hline(yintercept = as.numeric(YearLine),alpha=0.6,col="indianred1",linetype="dotted") + annotate("text", x = testdat$Combo, y = testdat$Start, label = testdat$Last, hjust=0,size=2.5) 

這裏是這樣的情節。這是那種我想要的(因爲它是不是多餘的),但我想它的贊助商細分:

Plot example

如果我添加+ facet_grid(Sponsor ~ .,scale="free_x",space="free_x")(或free_y),那麼它的贊助商作爲微面面板我希望,但我仍然得到所有的名字上市,即使他們不相關的是「贊助商」 B到E:

Faceted attempt

回答

1

您已經使用annotate,你需要使用的主要問題geom_text

ggplot(testdat,aes(Combo, Start, ymin=Start,ymax=End,color=as.factor(Grant),xticks)) + 
    xlab("Sponsor") + geom_linerange(size=4,alpha=.7) + geom_point(size=4,shape=18) + coord_flip() + 
    scale_colour_brewer(palette="Spectral") + 
    scale_x_discrete(labels=testdat$Sponsor) + 
    geom_hline(yintercept = as.numeric(YearLine),alpha=0.6,col="indianred1",linetype="dotted") + 
    geom_text(aes(x=Combo, y=Start, label = testdat$Last), colour="black") + 
    facet_grid(Sponsor ~ .,scale="free_x",space="free_x") 

annotate並沒有真正遵循你的情節,這就是爲什麼它不影響你的刻面的其餘部分定義的現有審美映射。

enter image description here

+0

謝謝。我試着用/不用'annotate'在那裏......但在你的情節中,即使是沒有名字的贊助商,我仍然可以看到所有* y的名字。例如在底部的面板E中,我不希望它在y(實際上是x翻轉)軸上顯示1-16的行......我希望'scale'參數將刪除這些行。我會嘗試一些更多的變體,而不用在裏面註釋... – beroe