1

我想将三个图形的组合保存为 pdf - 一个 ggplot 和两个 ggplotGrob。使用我正在尝试的代码,这些数字相互重叠。

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()
4

1 回答 1

0

你可以尝试拼凑:

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)

在此处输入图像描述

于 2021-08-17T13:43:25.870 回答