46

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,025
  • 18
  • 76
  • 114

3 Answers3

47
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
  • 30,900
  • 25
  • 101
  • 103
  • 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
41
ggplot() + theme_void()

No need to define a dataframe.

luchonacho
  • 6,045
  • 4
  • 30
  • 47
12

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 lable in the middle of it:

enter image description here

divibisan
  • 10,372
  • 11
  • 36
  • 56