R语言 如何使多个情节具有相同的大小?

cuxqih21  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(142)

我试图在R中绘制4个 Dataframe ,将它们排列为2行,第一行3,第二行1。我使用了以下代码:

library(ggplot2)
library(ggplotify)


# Loop to create 5 matrices and plot them
plot_list <- list()
grob_list <- list()
for (i in 1:4) {
  
  # Set seed for reproducibility
  set.seed(123 + i)
  
  # Create empty matrix
  mat <- matrix(nrow = 50, ncol = 4)
  
  # Set column names
  colnames(mat) <- c('a', 'b', 'c', 'd')
  
  # Set row names to random strings
  rownames(mat) <- replicate(50, paste(sample(letters, 5, replace = TRUE), collapse = ''))
  
  # Set values in matrix
  mat[sample(length(mat), 3)] <- runif(3, 10, 100)  # 3 values > 10
  
  # Identify the locations of the missing values
  missing <- is.na(mat)
  
  # Generate random values between 0 and 1 to replace the missing values
  replace <- runif(sum(missing), min = 0, max = 1)
  
  # Replace the missing values in the matrix with the random values
  mat[missing] <- replace
  
  # Convert mat to a data frame
  mat_df <- as.data.frame(mat)
  mat_df$names <- rownames(mat)
  
  # Reshape the data so that it can be plotted with ggplot
  mat_long <- reshape2::melt(mat_df, id.vars = "names")
  
  # Create the dotplot with text labels and proportional dot size
  p <- ggplot(mat_long, aes(x = names, y = value, fill = variable, size = value)) +
    geom_point(shape = 21, show.legend = FALSE) +
    scale_fill_manual(values = c("red", "green", "blue", "orange")) +
    scale_size_continuous(range = c(1, 10)) +
    theme_classic() +
    labs(x = '', y = "Average expression level", fill = "Columns") +
    facet_wrap(~ variable, ncol = 4, scales = "free_x", strip.position = "bottom") +
    guides(x.sec = "axis", y.sec = "axis") +
    theme(strip.placement = "outside",
          strip.background = element_blank(),
          strip.text = element_text(size = 14),
          panel.spacing = unit(0, "cm"),
          panel.grid.major.x = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.border = element_rect(colour = "black", fill=NA, size=1),
          axis.line = element_line(size = 1),
          axis.ticks.x = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks.y = element_line(colour = "black"),
          text = element_text(size=20)) +
    geom_text(data = subset(mat_long, value > 10), aes(label = names),
              nudge_x = 0, nudge_y = 2, size = 3, check_overlap = TRUE)
  
  # Print plot
  print(p)
  
  # Add plot to plot list
  plot_list[[i]] <- p
  grob_list[[i]] <- as.grob(plot_list[[i]])
  
}

# Arrange plots into a 2x3 grid for the first row and a 2x1 grid for the second row
grid.arrange(
  arrangeGrob(grobs=grob_list[1:3], ncol = 3),
  arrangeGrob(grobs=grob_list[4:5], ncol =1)
)

结果图显示,第一行的3个小区大小相同,但第二行的小区比第一行的大。你能告诉我怎样使这四块地都一样大吗?
非常感谢

omvjsjqw

omvjsjqw1#

选项1

安排ggplots最简单的方法是使用ggpubr::ggarrange,我认为ggpubr是最好的ggplot 2附加包之一,特别是对于科学出版物的东西。我认为ggplotify在这里是错误的调用,因为它用于将其他plot对象转换为ggplot 2-但无论如何,您只是使用ggplot对象。
使用ggpubr:

library(ggpubr)

ggarrange(plotlist = plot_list,ncol = 3,nrow = 2)

选项二

gridExtra软件包让您完全控制ggplot的排列。

gridExtra::grid.arrange(plot_list[[1]],
                        plot_list[[2]],
                        plot_list[[3]],
                        plot_list[[4]],
                        ncol=3,
                        nrow=2)

为了获得所需的输出(4个图),我将for循环范围从1:5更改为1:4(r使用基于1的索引,并将上限包含在范围中。

相关问题