ggplot2轴标签的分组

4

我正在尝试使用ggplot2构建一个图表,其中在X轴上,我可以找到一些方法来为变量组的标签。以下是我的代码的最小版本:

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

我已经尝试了比例尺、分段和标签的调整,但都无济于事。即使大量谷歌搜索也没有找到带有那种轴的图表。有没有办法得到我想要的效果呢?
1个回答

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


@Laurent 你确定吗?那看起来就像你画的图片一样。 - joran
@joran 我之前没有看到回复的更新。这太完美了! - Pepin_the_sleepy
@Didzis 这正是我想要的,之前不知道有 geom_segment。非常感谢! - Pepin_the_sleepy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接