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
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.
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.
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.
To customize the plot, the following arguments can be used: alpha, color, linetype, shape, size and fill.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With