NelnewR
NelnewR

Reputation: 131

Distorted image when adding text outside ggplot facets with gridExtra

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!

Upvotes: 0

Views: 142

Answers (1)

user9966263
user9966263

Reputation: 26

grid.arrange(p, bottom = sub.label)

Upvotes: 1

Related Questions