【问题标题】:how to change titles in ggplot for series of plots?如何更改 ggplot 中的标题以获取一系列情节?
【发布时间】:2021-01-05 15:38:52
【问题描述】:

我创建了一个函数,它按组变量“gear”分割数据帧,并为每个新的 df 绘制图。那么如何更改每个情节的标题?

    rank20 <- function(df){
      sdf <- df
      clusterName <- paste0("cluster",sdf$gear)
      splitData <- split(sdf,clusterName)
      plot <- lapply(splitData, function (x) {ggplot(x, aes(mpg, cyl)) + geom_point()+
          labs(x="mpg", y="cyl",
               title="This Needs To Be Changed") + 
          theme_minimal()})
      do.call(grid.arrange,plot)
    }
    rank20(mtcars)

我想要以下标题:gear3、gear4等(对应它们的gear值)

UPD:如果使用 mtcars,这两个结果都是正确的。但在我的真实情况下,我转换了我的初始df。所以,我应该以不同的方式提出我的问题。我需要从 splitData df 名称本身而不是从列中获取标题。

【问题讨论】:

  • 未测试,但您是否尝试过mapply()SIMPLIFY = FALSE 参数?
  • 嘿@Yulia Kentieva,如果能用data(mtcars)data(diamonds) 这样的公共数据集重现这个函数会很棒,也很高兴看到您的预期输出。
  • @AlvaroMartinez 谢谢!我把它改成了mtcars。我希望现在更好

标签: r function ggplot2


【解决方案1】:

我发现lapply 更容易使用索引作为输入。如果您需要将列表链接到另一个元素(例如列表的名称),则提供更大的灵活性:

rank20 <- function(df, col="gear"){
  sdf <- df
  clusterName <- paste0("cluster", sdf[[col]])
  splitData <- split(sdf, clusterName)
  # do the apply across the splitData indexes instead and pull out cluster
  # name from the column
  plot <- lapply(seq_along(splitData), function(i) {
    X <- splitData[[i]]
    i_title <- paste0(col, X[[col]][1])
    ## to use clusterName instead eg cluster3 instead of gear3:
    #i_title <- paste0(col, names(splitData)[i])
    ggplot(X, aes(mpg, cyl)) + 
      geom_point() +
      labs(x="mpg", y="cyl", title=i_title) + 
      theme_minimal()
  })
  do.call(grid.arrange,plot)
}
rank20(mtcars)

正如您所说,您希望名称为 gear2gear3,我已经同意了,但已经淘汰了使用 clusterName 值的替代 i_title

在函数的输入中,您可以更改 col 值以切换到不同的列,因此 gear 不是硬编码

【讨论】:

  • 谢谢,它适用于 mtcars。但是我更改了初始数据框。我应该以不同的方式提出我的问题。我需要从 splitData df 名称本身而不是从列中获取标题。这可能吗?
  • 是的,这就是答案。有关该选项,请参阅散列线 #i_title &lt;- paste0(col, names(splitData)[i]) :)
  • 哦,我没看到。谢谢!它有效!
【解决方案2】:

我可以建议一种稍微不同的方法(但也可以使用user Jonny Phelps answer 中建议的索引循环)。我正在创建一个绘图列表,然后使用 patchwork::wrap_plots 进行绘图。我觉得它更流畅。

library(tidyverse)
library(patchwork)

len_ind <- length(unique(mtcars$cyl))

ls_plot <-
  mtcars %>%
  split(., .$cyl) %>%
  map2(1:len_ind, ., function(x, y) {
    ggplot(y, aes(mpg, cyl)) +
      geom_point() +
      labs(x = "mpg", y = "cyl", 
           title = names(.)[x]
      ) +
      theme_minimal()
  })

wrap_plots(ls_plot) + plot_layout(ncol = 1)

刚刚注意到这是错误的列 - 使用 cyl 而不是 gear。哎呀。把它包装成一个函数很有趣:

plot_col <- function(x, col, plotx, ploty){
len_ind <- length(unique(mtcars[[col]]))
x_name <- deparse(substitute(plotx))
y_name <- deparse(substitute(ploty))

ls_plot <-
  mtcars %>%
  split(., .[[col]]) %>%
  map2(1:len_ind, ., function(x, y) {
    ggplot(y, aes({{plotx}}, {{ploty}})) +
      geom_point() +
      labs(x = x_name, y = y_name,
           title = names(.)[x]
      ) +
      theme_minimal()
  })

wrap_plots(ls_plot) + plot_layout(ncol = 1)
}

plot_col(mtcars, "gear", mpg, cyl)

【讨论】:

  • 谢谢,它适用于 mtcars。但是我更改了初始数据框。我应该以不同的方式提出我的问题。我需要从 splitData df 名称本身而不是从列中获取标题。这可能吗?
  • @YuliaKentieva 不太清楚你的意思。在 mtcars 的示例中,您希望在标题中出现什么名称?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2021-04-22
  • 2015-06-07
相关资源
最近更新 更多