轴标签ggplot2的分组 [英] grouping of axis labels ggplot2

查看:680
本文介绍了轴标签ggplot2的分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2绘制阴谋,在X轴上我可以找到一些变量组的标签。这里是我的代码的最小版本:

pre $ lt; code> Bzero< -100 * matrix(runif(100),ncol = 10, n = 10)

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

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


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

gg0 < - ggplot(mB0,aes(x = vid,y =变量))
gg0 < - gg0 + geom_tile(aes(fill = value),color =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')

下面是结果图:





以下是我想要的:



我一直在修整刻度,中断和标签为no无济于事。即使是大量的谷歌搜索也没有显示出这种轴的任何情节。有什么办法可以得到我想要的? 使用 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))


I am trying to bluid a plot with ggplot2 where on the X-axis I could find some way of having a label for groups of variables. Here is a minimal version of my code:

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')

Here's the resulting plot:

And here is what I'd like to have:

I have been tinkering with the scale, breaks, and labels to no avail. Even a massive amount of googling did reveal any plot with that kind of axis. Is there any way to get what I want?

解决方案

You can replace numbers with groups using scale_x_continuous() and setting breaks at desired positions. With geom_segment() you can add those black lines to group data.

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"))

这篇关于轴标签ggplot2的分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆