57

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?

3 Answers 3

56
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()
Sign up to request clarification or add additional context in comments.

1 Comment

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.
54
ggplot() + theme_void()

No need to define a dataframe.

Comments

17

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.