1

I have the following code:

df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),
              roi=rep(1:10,10),
              area=runif(100, 5.0, 7.5))

df$time=factor(df$time, levels=rev(levels(df$time)))  

ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
   theme_minimal() + coord_fixed(ratio=1) + 
   geom_tile(colour = NA, width = 1.5, height = 1) + 
   scale_fill_gradient(low="black",high="white")

Now, I want to remove the x-axis and add a new one to have the expected fig below. The x-axis will be divided into 4 parts for 4 segs with 39%,23%,23%,15% of axis length for Seg 1, Seg 2, Seg 3, Seg 4, respectively. Could anybody hava any idea to solve it. I apprecicate all response and am looking forward your answers.

Great thanks to Mark Heckmann for helpful answer to my problem. I would like to ask one more thing. I also want to modify the y-axis by "scale_y_discrete", the code ran well but the y-axis label did not meet my expectation. The code I ran is:

ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + theme_minimal() +coord_fixed(ratio=1) +geom_tile(colour = NA, width = 1.5, height = 1)+scale_fill_gradient(low="black",high="white") +scale_y_discrete(name="Time (min)",expand =c(0.01,0.1),breaks=c(1,2.5,5.0,7.5,10.0),labels=c(0,15,30,45,60))

Thank you very much!

enter image description here

Hoang Le
  • 308
  • 1
  • 3
  • 14

2 Answers2

1

This is as good as I can get without going into custom annotation grobs.

library(ggplot2)
library(grid)
df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),
              roi=rep(1:10,10),area=runif(100, 5.0, 7.5)) 
df$time=factor(df$time, levels=rev(levels(df$time)))
p1 <- ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
  theme_minimal() +coord_fixed(ratio=1) +
  geom_tile(colour = NA, width = 1.5, height = 1)+
  scale_fill_gradient(low="black",high="white") + 
  scale_x_discrete(breaks = c(4,6,8,10),
                   labels = paste0("Seg",1:4)) +
  theme(axis.title.x = element_blank(),
        axis.ticks.x = element_line(size =1),
        axis.text.x = element_text(hjust=c(2,1.5,1.5,1.5)),
        plot.margin = unit(c(2,0,2,0), "lines"))

See here if you want to look into drawing the axis labels and tick marks customwise.

Community
  • 1
  • 1
shayaa
  • 2,787
  • 13
  • 19
1

You need to use annotation_custom to draw outside the plotting area.

#### your plot

library(ggplot2)

g <- ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
  theme_minimal() + coord_fixed(ratio=1) +
  geom_tile(colour = NA, width = 1.5, height = 1) + 
  scale_fill_gradient(low="black",high="white") 

Extra code:

library(grid)

# remove axis 
g <- g +  theme(axis.title.x=element_blank(),
             axis.text.x=element_blank(),
              axis.ticks.x=element_blank()) +
          scale_x_discrete(expand = c(0,0)) 

# calculate segment coordinates
segs <- c(.39, .23, .23, .15)
segs_center <- cumsum(segs) - segs/2
seg_ticks <- cumsum(segs)[1:3]
seg_labels <- paste("Seg", seq_along(segs))

# create graphicaal objects and gather as tree
grobs <- grobTree(linesGrob(c(0,1), c(.5,.5)),
                  segmentsGrob(x0=seg_ticks, x1=seg_ticks, y0=0, y1=1),
                  textGrob(x=segs_center, y=0, 
                           label = seg_labels, hjust = .5, gp = gpar(cex =.7)))

# insert grobsTree in as annotation
g <- g + annotation_custom( grob = grobs,  
                            ymin = -.2, ymax = .2, 
                            xmin = .25, xmax = 10.75)

# override clipping for plotting outside of plotting area
gt <- ggplot_gtable(ggplot_build(g))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.newpage()  
grid.draw(gt)

enter image description here

Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
  • Yeah, that what I want. Thank you so much. – Hoang Le Aug 07 '16 at 09:48
  • @ Mark, when I ran that code, I got this error: Error in layer(data = NULL, stat = StatIdentity, position = PositionIdentity, : object 'axis.y.pos' not found and the plot I had with nothing in x-axis. Do you know why? Thanks a lot. – Hoang Le Aug 07 '16 at 10:58
  • @HoàngThịMỹDungLê Forgot to remove a constant. Fixed now. – Mark Heckmann Aug 07 '16 at 11:04
  • H. Thanks a lot :) – Hoang Le Aug 07 '16 at 11:14
  • Sorry, I forgot doing it. Thanks for your request. I still have another problem to solve on the y-axis. Could you spent a bit time to read my question again (the content below) and it would be really nice if you could help me one more. If not, I still appreciate what you have done so far and spent your time on my question. Thanks again! – Hoang Le Aug 07 '16 at 11:34
  • Consider posting a new question, as it is a somewhat different problem. – Mark Heckmann Aug 07 '16 at 15:02