Markm0705
Markm0705

Reputation: 1440

How to plot text in the margin using ggplot2?

Using base R it is relatively easy to plot to include the plot file name or other useful metadata on a plot margin using mtext(). However, I have started using ggplot2 for some images and I cannot seem to find a simple example to follow.

Below is some example code where I would like to include the file name ('Fn') in the RHS of the lower margin of the plot.

library(ggplot2)
ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() 
Fn <- 'Diamods-carat-v-price.png'
ggsave(Fn, device = 'png', width = 30, height = 20, 
       units = 'cm', dpi = 320 )

Upvotes: 0

Views: 463

Answers (1)

william3031
william3031

Reputation: 1708

Like this?

library(ggplot2)
Fn <- 'Diamods-carat-v-price.png'
ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() +
  labs(caption = paste0(Fn))

ggsave(Fn, device = 'png', width = 30, height = 20, 
       units = 'cm', dpi = 320 )

enter image description here

Upvotes: 2

Related Questions