2013-05-02 38 views
2

我試圖用可置於「A0」和「A1」中間的一個「A」替換x軸標籤「A0」和「A1」 」。如果存在一種類似於以下問題的方法會更好: grouping of axis labels ggplot2當x是ggplot2中的因子變量時對標籤進行分組

因此,我的意思是隻爲每個組重新繪製x軸,並在組之間留空。

這裏是我工作的代碼:

y = 1*round(runif(20)*10,1) 
x1 = c("A","B") 
x2 = c(0,1) 
x = expand.grid(x1,x2) 
xy = cbind(x,y) 
xy$z = paste(xy$Var1,xy$Var2,sep="") 

p <- ggplot(xy, aes(x=factor(z), y=y,fill=factor(Var2))) 
p + geom_boxplot() + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C")) 

回答

2

試試這個。無需變量z,只需使用position="dodge"

p <- ggplot(xy, aes(x=factor(Var1), y=y,fill=factor(Var2))) 
p + geom_boxplot(position="dodge") + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C")) 
+0

它的工作原理。非常感謝你。 – Tony 2013-05-02 16:33:49

相關問題