R Question

Remove spaces between bars in barplot 2 factors

I have code following:

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

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

ggplot(data=df, aes(y=factor(roi), x=time, fill = area)) + theme_minimal() + coord_flip() + geom_tile(colour = "white", width = 0.9, height = 0.5) + scale_fill_gradient(low="blue",high="red")

How can I remove the spaces between bars of 10 rois (I mean the spaces between rois) and make 10 bars of 10 rois stay continuously for each value of time. Thank you very much for any help.

Answer Source
  • Instead of the height and width arguments to geom_tile, use coord_fixed(ratio=r) to control the aspect ratio of the tiles (where r is the ratio). I've used r=0.5 in the example below.
  • Switch the x and y columns so that you get roi on the x-axis without needing coord_flip() (you can't use both coord_flip() and coord_fixed() at the same time).
  • Set colour=NA so that there won't be a space between each tile within a given row.
  • Set height=0.9 to create a space between each row of tiles.

    ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
      theme_minimal() + 
      coord_fixed(ratio=0.5) +
      geom_tile(colour = NA, width = 1, height = 0.9) + 

enter image description here