【问题标题】:Creating multiple ggplots with dplyr使用 dplyr 创建多个 ggplots
【发布时间】:2018-09-20 23:29:11
【问题描述】:

使用GGally::ggpairs() 创建绘图矩阵后,我想存储各个散点图以供以后使用。

这是我当前的代码:

# load necessary package
library(GGally) # loads `ggplot2` 
library(magrittr) # allows for the use of `%>%`

# create a matrix of plots
mtcars %>%
  na.omit() %>%
  ggpairs(columns = 1:7)

# how do I automate this process?
P1 <- ggplot(aes(x = disp, y = hp)) + 
        geom_point()
P2 <- ggplot(aes(x = drat, y = hp)) + 
        geom_point()
P3 <- ggplot(aes(x = hp, y = qsec)) + 
        geom_point()

我收到一条错误消息,指出数据必须是数据框。我尝试使用. 指定来自na.omit() 管道的数据,但我收到了相同的结果。

感谢任何建议!

【问题讨论】:

  • 请提供部分或全部UN3 以提出此问题reproducible
  • 在您的代码示例中,管道在 ggpairs 行结束。在接下来的 3 行中,您没有向 data = 参数传递任何内容(也许您的意思是 data = mtcars ?)。代码的意图不明确。
  • @neilfws 道歉我试图缩小代码以使其更具可读性,但忘记进行一些更改。我认为问题在于管道操作员在第一个 ggpairs 函数之后停止提供输出。我对你的理解正确吗?有没有办法引用每个ggplot中的数据?
  • 正如 neilfws 所指出的,在将数据集传递给 ggpairs() 后,您还不清楚您打算做什么。其输出是 gg 对象,而不是数据框。你的意思是你想覆盖不同的geom_point() 层吗? (如果是这样,请包含您所需输出的草图。)您是否只想将数据集传递给ggplot() 并添加三个不同的geom_point() 层? (如果有,ggpairs() 在代码中做了什么?)
  • @Z.Lin 我的意图是创建一个ggpairs() 对象并独立创建ggplot 对象以供以后使用。这可能吗?或者它要求太多了。为了清楚起见,我做了一些修改。

标签: r ggplot2 ggally


【解决方案1】:

概述

我将所有单独的 ggplot(...) 调用压缩为一个自定义函数:ScatterPlot()

然后我创建了另一个自定义函数 ManyScatterPlots() - 它使用 purrr::map() - 将每个特定列的单独散点图存储在 x 轴上的 df 和列表中 y 轴上的每一列。对于df 中的每一列,此过程都会重复。

ManyScatterPlots() 的结果是一个列表列表,其中每个单独的列表都包含许多散点图。我已经标记了列表列表和各个图,以便以后更容易找到您要查找的内容。

# load necessary package -----
library(tidyverse)

# create a function that makes one scatter plot
ScatterPlot <- function(df, x, y) {
  # Input: 
  #     df: a data frame
  #     x: a column from df in the form of a character vector
  #     y: a column from df in the form of a character vector
  #
  # Output:
  #     a ggplot2 plot
  require(ggplot2)

  ggplot(data = df, aes(x = get(x), y = get(y))) +
    geom_point() +
    xlab(x) +
    ylab(y) +
    labs(title = paste0(y, " as explained by ", x))
}

# create a function that plots one ScatterPlot() for every possible column combination  -------
ManyScatterPlots <- function(df) {
  # Input: 
  #     df: a data frame
  #
  # Output:
  #     a list of ggplot2 plots from ScatterPlot()
  require(magrittr)
  require(purrr)

  # for each column in df
  # create an individual scatter plot for that column on the x-axis
  # and every column on the y-axis
  colnames(df) %>%
    map(.f = function(i) 
      map(.x = colnames(df), .f = function(j)
        ScatterPlot(df = df, x = i, y = j)) %>%
        # to help identify the individual plots for that particular column
        # label the plots inside the list
        purrr::set_names(nm = paste0(colnames(df)
                                     , " as explained by "
                                     , i))) %>%
    # to help identify the list of plots for each particular column
    # label the plots inside the list
    purrr::set_names(nm = colnames(df))
}

# use ManyScatterPlots() -----
many.plots <- ManyScatterPlots(df = mtcars)

# view results ---
names(many.plots)           # a list of lists
map(.x = many.plots, names) # a list of individual scatter plots
many.plots$disp$`hp as explained by disp`
many.plots$drat$`hp as explained by drat`
many.plots$hp$`qsec as explained by hp`

# end of script #

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    相关资源
    最近更新 更多