【问题标题】:PCA plot and Proportion of Variance table in one ggplot2 plot - R一个ggplot2图中的PCA图和方差表的比例 - R
【发布时间】:2020-11-19 20:36:13
【问题描述】:

我有一个 PCA 图和方差比例表。当我在绘图上方绘制提到的表(使用 tableGrob)时,它们之间有很多空间。如何将它们“更近”地连接并将表格与 PCA 图的右上方对齐?

library(data.table)
library(MASS)
library(ggplot2)
library(reshape2)
library(grid)
library(gridExtra) 
   
#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))
 
grid.arrange(propPlot, mainPlot, ncol=1)

【问题讨论】:

  • 可能是cowplot::plot_grid(propPlot, mainPlot, nrow = 2, rel_heights = 1:4)

标签: r ggplot2 pca


【解决方案1】:

您需要对代码进行的唯一更改是在 grid.arrange() 函数中。

  • 我将ncol = 1 替换为nrow = 2
  • as.table = TRUE 已添加
  • heights = c(1, 3) 已添加(行的相对高度)
grid.arrange(propPlot, mainPlot, nrow = 2, as.table = TRUE, heights = c(1, 3))

这是你将得到的:

如果没有这篇博文,我无法找到解决方案:https://magesblog.com/post/2015-04-14-plotting-tables-alsongside-charts-in-r/

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2021-05-13
    • 1970-01-01
    • 2015-12-15
    • 2013-08-06
    • 2019-12-09
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多