r - 使用带有标签和 common.legend 的 grid.arrange 排列多个 ggplots
问题描述
如何绘制带有标记(A 和 B)和常见图例的两个图形?我使用了 ggarrange {ggpubr},它似乎不支持 gridExtra 图。
library(data.table)
library(ggplot2)
library(grid)
library(gridExtra)
library(ggpubr)
P1 <- ggplot(iris,aes(x=Sepal.Length,y=Petal.Length)) + geom_point(size = 2, aes(color=Species))
P2 <- ggplot(iris,aes(x=Petal.Width,y=Petal.Length)) + geom_point(size = 2, aes(color=Species))
#PCA
iris.pca <- prcomp(iris[,1:4], scale. = TRUE)
dataIris.pca <- data.frame(summary(iris.pca)$importance)
dat <- data.table(PC1=iris.pca$x[,1],PC2=iris.pca$x[,2],Species= iris[,5])
dat <- dat[order(dat$Species),]
#PCA plot
mainPlot <- ggplot(dat,aes(x=PC1,y=PC2)) + geom_point(size = 2, aes(color=Species))
mainPlot
#Prop variance table
Prop <- as.data.frame(summary(iris.pca)[[6]])
PropTable <- round(Prop[2,],3)
#Prop variance plot
propPlot <- tableGrob(PropTable,theme = ttheme_default(base_size = 8))
P3 <- grid.arrange(propPlot, mainPlot, nrow = 2, as.table = TRUE, heights = c(1, 3))
ggarrange(P1, P2, P3, labels = c("A", "B", "C"), ncol=3, nrow=1, common.legend = TRUE, legend="bottom")
解决方案
推荐阅读
- javascript - 如何为此表的单元格创建工作标识符?
- azure - Azure 数据工厂存储变量到 blob 中的文件
- java - 将 Apache Cassandra 与基于 Java 11 的 Java 应用程序一起使用?
- angular - 从一个 Angular 9 项目创建多个应用程序
- javascript - 无法 POST 成功的 ajax 请求
- mysql - 使用聚合函数 mySQL 时不断出现错误
- git - 如何在 Mac 上使用 Git 和 NPM 安装此软件
- php - Symfony Doctrine 捕获连接异常
- java - 从另一个容器内的 Java 应用程序引用 docker 容器内的 MySQL 服务器
- reactjs - 我应该构建一个本地数据层/应用程序状态来维护 React Native/Firestore 应用程序中的状态吗?