Pepin_the_sleepy
Pepin_the_sleepy

Reputation: 297

grouping of axis labels ggplot2

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:

Resulting plot:

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

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?

Upvotes: 4

Views: 2454

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98589

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

enter image description here

Upvotes: 5

Related Questions