52

This question demonstrates how to put an equation into a ggplot2 qplot.

q <- qplot(cty, hwy, data = mpg, colour = displ)
q + xlab(expression(beta +frac(miles, gallon)))

How could I create an empty ggplot2 plot so that I could add the label to an empty canvas?

Community
  • 1
  • 1
Jim
  • 11,229
  • 20
  • 79
  • 114

3 Answers3

53
df <- data.frame()
ggplot(df) + geom_point() + xlim(0, 10) + ylim(0, 100)

and based on @ilya's recommendation, geom_blank is perfect for when you already have data and can just set the scales based on that rather than define it explicitly.

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_blank()
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • 1
    Yeah, that works and I guess that answers my question. I suppose the question I should have asked was how to create a plot that that consists only of a label. – Jim Sep 20 '12 at 18:26
48
ggplot() + theme_void()

No need to define a dataframe.

luchonacho
  • 6,759
  • 4
  • 35
  • 52
15

Since none of the answers explicitly state how to make a plot that consists only of a label, I thought I'd post that, building off the existing answers.

In ggplot2:

ggplot() +
    theme_void() +
    geom_text(aes(0,0,label='N/A')) +
    xlab(NULL) #optional, but safer in case another theme is applied later

Makes this plot:

enter image description here

The label will always appear in the center as the plot window resizes.

In cowplot:

cowplot::ggdraw() +
    cowplot::draw_label('N/A')

ggdraw() makes a blank plot while draw_label draws a label in the middle of it:

enter image description here

divibisan
  • 11,659
  • 11
  • 40
  • 58