0

Im trying to use the annotate_figure function to add a title to my graph. I'm currently running this code :

p3<- ggplot(SPPLongForm, aes ( x =Score, fill = Time))+
  geom_bar(position ="identity")+
  theme_classic()+
  scale_fill_manual(values = c("cadetblue4", "darkred"))+
  theme(legend.position="top") +
  theme(strip.text.x=element_blank())+
  scale_x_continuous (breaks = seq (1,7,1))+
  facet_grid(.~Time)+
  labs(x= "Mean Score", fill ="", y = "Count")


annotate_figure(p1, left=text_grob("Socially Prescribed Perfectionism Subscale"))+
  ggsave(file = "p1.png", p1, dpi = 700)

My graph runs correctly, but when I run the annotate_figure function I receive the warning "Error: Can't add e2 to a ggplot object." . I'm not sure what this error is telling me

Dan Adams
  • 4,971
  • 9
  • 28
zucchini
  • 33
  • 4
  • 2
    You should not be using `+` with ggsave. The `+` at the end of the `annotate_figure` line seems out of place. Plus `annotate_figure` will return a new object; it will not modify `p1` in place. You'll need to save the result to a new variable that you pass to `ggsave`. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Feb 15 '22 at 16:27
  • Ah I see, when I made the annotated figure a new object and then ran ggsave separately it worked. Thank you :) – zucchini Feb 15 '22 at 16:52
  • This is a known [off-label use](https://www.tidyverse.org/blog/2021/06/off-label-uses-in-ggplot2/) of `ggsave()` but is discouraged. – Dan Adams Feb 15 '22 at 16:58

0 Answers0