【问题标题】:ggplot multiple figures gridExtraggplot 多个数字 gridExtra
【发布时间】:2020-05-05 22:26:28
【问题描述】:

我知道关于这个话题已经有很多答案了。然而,对于一个新手来说,仍然有一些我无法绕过的步骤。所以我们开始吧。希望你能帮助我。

我想 2 到 2 排列四个不同的地块。我使用的是 ggplot,所以我不能使用 par(mfrow=c(2,2)) 但它基本上是我想做的。从我读过的内容来看,我应该使用 gridExtra。所以这是我的代码:

Plot_Graph <- function(DF, na.rm = TRUE){
  nm = names(DF)[-1]
  for (i in nm) {
   p <- ggplot(DF, aes(x = Date, y = get(i))) +
           geom_line() + 
           scale_x_date(minor_breaks = "1 year") +
           xlab("Year") + 
           ylab("Stock price US$") +
           ggtitle(paste(i)) +
           theme_bw()
   grid.arrange(p)
  }
}

数据样本:

structure(list(Date = structure(c(10960, 10961, 10962, 10963, 
10966), class = "Date"), AAPL = c(1, 1.01463414634146, 0.926829268292683, 
0.970731707317073, 0.953658536585366), GE = c(1, 0.998263888888889, 
1.01159722222222, 1.05076388888889, 1.05034722222222), SPY = c(1, 
1.00178890876565, 0.985688729874776, 1.04293381037567, 1.04651162790698
), WMT = c(1, 0.976675478152698, 0.990359197636448, 1.06515316436013, 
1.04571606282071)), row.names = c(NA, 5L), class = "data.frame")

我想我的问题确实是,我不知道我的地块存储在哪里,在执行循环时,所以我可以再次访问它们。

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案1】:

    您可以使用优秀的patchwork 包:

    library(ggplot2)
    library(patchwork)
    
    nm <-  names(DF)[-1]
    
    plots <- lapply(nm, function(x) {
      ggplot(DF, aes(x = Date, y = get(x))) +
        geom_line() + 
        scale_x_date(minor_breaks = "1 year") +
        xlab("Year") + 
        ylab("Stock price US$") +
        ggtitle(x) +
        theme_bw()
    })
    
    Reduce(`+`, plots) + plot_layout(nrow = 2)
    

    您也可以使用 tidyr::pivot_longer 和 facet:

    library(ggplot2)
    library(tidyr)
    
    DF %>% 
      pivot_longer(-Date) %>% 
      ggplot(aes(Date, value)) +
      geom_line() + 
      scale_x_date(minor_breaks = "1 year") +
      xlab("Year") + 
      ylab("Stock price US$") +
      theme_bw() +
      facet_wrap(~name)
    

    【讨论】:

    • 谢谢。我更喜欢两者中第一个的外观。我无法让代码工作。在我使用Plot_Graph(DFbasket) 调用该函数之前。我应该命名一个新函数吗?
    • 如果您只是在答案中运行代码,它应该会生成绘图,如果它给出错误,您可能需要安装拼凑或将您的数据框重命名为DF。如果您愿意,您可以轻松地将代码包装在一个将数据框作为参数的函数中
    • 这些图已保存在四个列表中,但当我打电话时没有任何反应:Reduce(+, plots[1:2]) / Reduce(+, plots[3:4]) 我也尝试过plot_grid(plots[[1]], plots[[2]], labels = "AUTO"),这是一样的。我也没有收到任何错误。
    • 我重新启动了 R 并使用了 plot_grid(plots[[1]], plots[[2]], labels = "AUTO") 工作。不知道为什么其他东西没有。
    【解决方案2】:

    你需要将它们放在一个列表中然后grid.arrange,并尽量不要使用get(),它有时会导致一些混乱(在我看来),我在下面使用了!!sym():

    Plot_Graph <- function(DF, na.rm = TRUE){
    nm = names(DF)[-1]
    plts = lapply(nm,function(i){
       p <- ggplot(DF, aes(x = Date, y = !!sym(i))) +
               geom_line() + 
               scale_x_date(minor_breaks = "1 year") +
               xlab("Year") + 
               ylab("Stock price US$") +
               ggtitle(paste(i)) +
               theme_bw()
        return(p)
       })
    grid.arrange(grobs=plts,ncol=2)   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多