Grouping axial marks ggplot2

I am trying to plot with ggplot2, where on the X axis I could find a way to have a label for groups of variables. Here is the 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 graph:

Resulting plot:

And here is what I would like to have: enter image description here

I did not deal with scaling, breaks and shortcuts. Even a huge number of search queries revealed any plot with such an axis. Is there a way to get what I want?

+4
source share
1 answer

You can replace numbers with groups using scale_x_continuous() and set breaks at desired positions. With geom_segment() you can add these black lines to group the 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")) 

enter image description here

+5
source

All Articles