1

I would like to save as pdf a combi of three graphs - one ggplot and two ggplotGrob. With the codes I am trying, the figures are on top of each other.

library(ggplot2)
library(condformat)
library(ggpubr)
data(iris)

graph1 <- ggplot(data=iris, aes(Sepal.Length, y=Sepal.Width, color=Species)) + 
  geom_point(aes(shape=Species), size=1.5) + geom_smooth(method="lm") +
  xlab("Sepal Length") + ylab("Sepal Width")
table1 <- condformat(iris[10:20, 1:4]) %>%
  rule_text_color(Sepal.Length, ifelse(Sepal.Length >= 4.6, "red", "")) %>%
  condformat2grob()

Plots <- ggarrange(graph1,                                                 
         ggarrange(table1, tableGrob(iris[10:20, 3:4]), ncol = 2, labels = c("B", "C")), 
                           nrow = 2, 
         labels = "A" , 
         heights=c(3,2,1))  
annotate_figure(Plots, 
                top = text_grob("Graphs", color = "black", face = "bold", size = 20),
                bottom = text_grob("Figure 1. write legend here.", 
                                   hjust = 1, x = 1, size = 9))
ggsave(filename="Plots.pdf", Plots, width=11, height=8.5)
dev.off()
CamillaM
  • 23
  • 5
  • Your example is not reproducible and I'm not investigating what packages need to be loaded in addition. However, my first thought would be that you need to adjust (increase) the size of the pdf. – Roland Aug 17 '21 at 10:36
  • Oh, sorry, I forgot to load some libraries – CamillaM Aug 17 '21 at 11:02

1 Answers1

0

you could try out patchwork :

library(ggplot2)
library(condformat)
library(ggpubr)
library(gridExtra)

# added:
install.packages("patchwork")
library(patchwork)
### 
data(iris)

graph1 <- ggplot(data=iris, aes(Sepal.Length, y=Sepal.Width, color=Species)) + 
  geom_point(aes(shape=Species), size=1.5) + geom_smooth(method="lm") +
  xlab("Sepal Length") + ylab("Sepal Width")
table1 <- condformat(iris[10:20, 1:4]) %>%
  rule_text_color(Sepal.Length, ifelse(Sepal.Length >= 4.6, "red", "")) %>%
  condformat2grob()

#added:
grob1<-tableGrob(iris[10:20, 3:4])
###

# Using patchwork:
big_plot<-  graph1 + 
            grob1 + 
            table1 +
            guide_area()+
            plot_layout(ncol = 2, guides = "collect") +
            plot_annotation(tag_levels = "A", title = "Graphs",
                            caption = "Some patchwork magic",
                            theme = theme(plot.title = element_text(size = 26)))

big_plot
ggsave(filename="Plots.pdf", big_plot, width=11, height=8.5)

enter image description here

user12256545
  • 2,755
  • 4
  • 14
  • 28