1

I have a situation (not the example here) where I want to put auxiliary information in a plot between the left vertical axis and the data that are actually plotted. Here is a toy example -- a dumb-looking plot, but illustrates the problem. First, a toy dataset:

toy = data.frame(group = rep(LETTERS[1:2], each = 5), 
    treat = rep(letters[1:5], 2),
    score = c(57, 68, 49, 88, 75,   52, 69, 54, 90, 72),
    time = c(23.5, 14.9, 64.2, 34.8, 31.1,   56.9, 45.5, 32.7, 22.0, 45.3))

And the code:

library(ggplot2)
ggplot(data = toy, aes(x = score, y = treat)) + 
    geom_tile() + geom_tile(aes(x = 32)) + 
    facet_grid(. ~ group) + 
    geom_label(aes_(x = 30, label = ~time, hjust = "right"))

enter image description here

At the scale shown, the labels are only partly visible. They are completely visible if I resize the plot window to full-screen. But I need the labels to fit and be fully visible between the left end of the data and the vertical axis for any reasonable sizing of the plot window.

I could add an invisible point at a smaller x value to make more room, but the labels are fixed size, so what x to use depends on how the plot is sized. Also, it would be nice if the size of these labels does not shrink if I make the window smaller -- just like the tick labels and so forth all stay the same size. (In fact, it's kind of like having an additional vertical axis in each panel, with different labels). Any suggestions?

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21
  • 1
    Use `expand_scale` in the `expand` argument of your x-axis scale to add more space on the left – camille Apr 04 '19 at 19:36
  • Thanks. I had found https://stackoverflow.com/questions/18028731/ggplot2-geom-bar-plot-labels-fall-outside-plot but it wasn't nearly as helpful. I'm glad to know this, but it still is an adjustment relative to scale coordinates, not physical ones. Thus, in my example, if I got the labels just right, then there'd be too much space at the left if I omit the `facet_grid` term - the panel would be twice as wide so the margin is twice as big, while the labels are the same size. I surmise that I can't mess with setting hardware units except by doing something delicate with the **grid** package. – Russ Lenth Apr 04 '19 at 21:25
  • You could also use `coord_cartesian(clip = "off")`. This won't bring the labels into the plotting area, but it will allow them to show up without clipping the edges off. – Jon Spring Apr 04 '19 at 21:31
  • You can give a vector to `expand` without using `expand_scale`. I'm not entirely sure how coordinates work in that case. – camille Apr 04 '19 at 21:35

0 Answers0