0

I am trying to add the file name outside a plot area obtained with ggplot, using facet_wrap. I was pretty sure I found the solution in this post: Add filename or other annotation to ggplot figures. However, applying the solution to my problem gives a distorted image. distorted plot

The code to generate this is here:

require("gridExtra")
library(tidyverse)

df <- data.frame(x =runif(100, 1, 10),
             y = runif(100, 1, 10),
             myfacet = c("one", "two"))
p <- ggplot(data = df,
        aes(x = x,
            y = y)) +
geom_point() +
facet_wrap(~myfacet)

print(p)
script.name <- "myscript.R"
sub.label = textGrob(script.name, 
                 gp=gpar(fontsize=6),
                 x = unit(1, "npc"),
                 hjust = 1,
                 vjust = 1)
ggsave(filename="../plots/myplot.png",
   plot = arrangeGrob(p,
                      sub = sub.label,
                      clip = FALSE))

If I just use

ggsave(filename="../plots/myplot2.png",
   plot = p)

I get the following image: enter image description here

Please note that I need a solution that works outside the facets. Could anyone provide a hint as to what is going on? Thank you!

NelnewR
  • 131
  • 7

1 Answers1

1
grid.arrange(p, bottom = sub.label)
  • Is there also a way to slightly move away the sub.label from the edge of the plot? If I have underscores in the label, this is otherwise very hard to read. – NelnewR Jun 20 '18 at 08:32