Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to annotate ggplot2 qplot outside of legend and plotarea? (similar to mtext())

I would like to annotate my plots with a filename. With plot() I used mtext:

plot(1:10)
mtext("File xy-12-34-56.csv", 4)

How can I do that with ggplot2 and qplot or ggplot? It should not collide with the legend. I found the commands annotate and grid, but I could not get an annotation similar to mtext with these.

As a workaround I could try watermarks, but perhaps you have a good hint for me. Kind regards, Jonas

like image 241
Jonas Stein Avatar asked Oct 22 '12 10:10

Jonas Stein


People also ask

How do I annotate a plot in R?

If you want to annotate your plot or figure with labels, there are two basic options: text() will allow you to add labels to the plot region, and mtext() will allow you to add labels to the margins. For the plot region, to add labels you need to specify the coordinates and the label.

How do I add a note in ggplot2?

You can use the annotate() function to add text to plots in ggplot2. where: x, y: The (x, y) coordinates where the text should be placed. label: The text to display.

What is the difference between Ggplot and Qplot?

The function qplot() [in ggplot2] is very similar to the basic plot() function from the R base package. It can be used to create and combine easily different types of plots. However, it remains less flexible than the function ggplot(). This chapter provides a brief introduction to qplot(), which stands for quick plot.

Which argument of Ggplot can be used to add customization to plots?

To customize the plot, the following arguments can be used: alpha, color, linetype, shape, size and fill.


1 Answers

Update

Looks like to achieve the result now we should use the following:

library(ggplot2)
library(grid)
library(gridExtra)
p <- qplot(data = mtcars, wt, mpg)
grid.arrange(p, right = textGrob("File xy-12-34-56.csv", rot = -90, vjust = 1))

Old answer

Try this:

library(gridExtra)
p <- qplot(data = mtcars, wt, mpg)
print(arrangeGrob(p, legend = textGrob("File xy-12-34-56.csv", rot = -90, vjust = 1)))

enter image description here

like image 110
Julius Vainora Avatar answered Oct 15 '22 18:10

Julius Vainora