0

I'm producing a geom_tile ggplot and my x-axis tick labels are long:

set.seed(1)
df <- data.frame(sample = c(rep(paste(rep("S1",30),collapse=""),30),
                            rep(paste(rep("S2",30),collapse=""),30),
                            rep(paste(rep("S3",30),collapse=""),30)),
                 gene = rep(paste("G", 1:30, sep = ""), 90),
                 value = rnorm(90))

So when I produce the plot they get cut off:

require(ggplot2)

ggplot(df, aes(x = sample, y = gene)) + 
    geom_tile(aes(fill = value)) + 
    scale_fill_gradient2(high = "darkred", low = "darkblue") +
    theme_bw() + 
    theme(legend.key = element_blank(), 
          legend.position = "right", 
          axis.text.y = element_blank(), 
          axis.ticks.y = element_blank(), 
          panel.border = element_blank(), 
          strip.background = element_blank(), 
          axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

enter image description here

How can I fix this?

alistaire
  • 42,459
  • 4
  • 77
  • 117
dan
  • 6,048
  • 10
  • 57
  • 125
  • 1
    put some line breaks in there? – Nate Jan 20 '17 at 00:52
  • I prefer to expand the area of the plot so it wraps the labels – dan Jan 20 '17 at 00:53
  • will the the `str_wrap` answer here work? http://stackoverflow.com/questions/21878974/auto-wrapping-of-labels-via-labeller-label-wrap-in-ggplot2 – Nate Jan 20 '17 at 00:54
  • If for some reason you don't want to wrap the labels, then you could increase the left margin of the plot, though that will also result in a lot of empty white space above the diagonal label. If you want to go with that option add `theme(plot.margin=margin(5,5,5,50,unit="pt"))`, (or whatever margin values work for you) where the order of the margin values is top, right, bottom, left. The default units are "pt", but you can use "lines", "in", or "cm" as well. – eipi10 Jan 20 '17 at 01:01
  • 1
    As far as I understand str_wrap will work character vectors of length > 1, whereas my labels have length = 1. In addition, personally I think that expanding the plot are rather than breaking down the labels is a more elegant solution – dan Jan 20 '17 at 01:02
  • Thanks @eipi10. I don't suppose ggplot can figure out automatically how much margin I need to include the labels in the plot area? – dan Jan 20 '17 at 01:03
  • 1
    If you want to get fancy, you could probably use the width of the string (`strwidth` is the base graphics function, but you probably need a grid graphics function for this case) its angle and the distance of the tick mark from the edge of the plot area to figure out the amount of overhang programmatically. – eipi10 Jan 20 '17 at 01:09
  • 1
    `str_wrap` from the `stringr` package will wrap an individual string. There also [base `strwrap` combined with `paste`](http://stackoverflow.com/a/29847221/496488). – eipi10 Jan 20 '17 at 01:41
  • 3
    Just flip your x and y aesthetics and ditch the angle so people aren't cocking their heads to read your plot. – alistaire Jan 20 '17 at 02:08
  • Please, see my answer to the Q [_How to maintain size of ggplot with large legends_](http://stackoverflow.com/a/41607201/3817004) for different possibilities to deal with long labels. – Uwe Jan 20 '17 at 05:07

0 Answers0