2013-03-07 657 views
4

我想用ggplot2繪製一個陰謀,在X軸上我可以找到一些變量組標籤的方法。這裏是我的代碼最低版本:軸標籤的分組ggplot2

Bzero <-100*matrix(runif(100),ncol=10,nrow=10) 

B <-99 
LNtype <-c(1,1,1,1,2,2,2,3,3,3) 
LNnames <-c('grp1','grp2','grp3') 

tB <-t(Bzero)/(B+1) 
dfB <-data.frame(tB) 
dfB$grp <-LNtype 
dfB$vid <-1:nrow(tB) 


mB0 <- melt(dfB,id.vars=c('grp','vid')) 
mB0 <- mB0[order(mB0$grp,mB0$vid),] 

gg0 <- ggplot(mB0,aes(x=vid,y=variable)) 
gg0 <- gg0 + geom_tile(aes(fill = value),colour = "white") 
gg0 <- gg0 + scale_fill_gradient(low = "green", high = "red",na.value='white',limits=c(0,1),name='p0i') 
gg0 <- gg0 + xlab('Equation')+ylab('Covariate') 

下面是導致情節:

Resulting plot:

這裏是想什麼,我有: enter image description here

我一直與規模修補,打破和標籤無濟於事。即使是大量的谷歌搜索也沒有顯示出這種軸的任何情節。有什麼辦法得到我想要的?

回答

5

您可以使用scale_x_continuous()替換組中的數字,並在所需的位置設置中斷。使用geom_segment(),您可以將這些黑線添加到組數據。

gg0+ 
geom_segment(aes(x=0.5,y=0.5,xend=10.5,yend=0.5))+ 
geom_segment(aes(x=c(0.5,4.5,7.5,10.5), 
        xend=c(0.5,4.5,7.5,10.5),y=rep(0.5,4),yend=rep(1,4)))+ 
    scale_x_continuous("",breaks=c(2.5,6,9),labels=c("Group1","Group2","Group3")) 

enter image description here

+0

@Laurent你確定嗎?這看起來完全像你畫的圖片。 – joran 2013-03-07 17:23:39

+0

@joran我沒有看到回覆的更新。太棒了! – 2013-03-07 17:28:26

+0

@Didzis這正是我想要的,不知道geom_segment。非常感謝! – 2013-03-07 17:30:25