使用dplyr创建多个ggplot图表

3

使用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() 管道中的数据,但结果仍然相同。
非常感谢您的任何建议!

1
请提供一些或全部的UN3,以使这个问题能够可复现 - neilfws
1
在你的代码示例中,管道在 ggpairs 行结束。在接下来的3行中,你没有向 data = 参数传递任何东西(也许你的意思是 data = mtcars?)。代码的意图不明确。 - neilfws
@neilfws 抱歉,我试图缩小代码以使其更易读,但忘记了进行一些更改。我认为问题在于管道运算符在第一个ggpairs函数之后停止提供输出。我理解得对吗?有没有办法引用每个ggplot中的数据? - samvoit4
正如Neilfws所指出的那样,传递数据集到ggpairs()之后你的意图并不清楚。从中得到的输出是一个gg对象,而不是一个数据框。你是想要将不同的geom_point()图层叠加到上面吗? (如果是这样的话,请包括你所期望的输出的草图。)还是你只是想将你的数据集传递给ggplot(),并添加三个不同的geom_point()图层? (如果是这样的话,那么在代码中ggpairs()有什么作用呢?) - Z.Lin
1
@Z.Lin 我的意图是创建一个 ggpairs() 对象,并独立地创建 ggplot 对象以供稍后使用。这可能吗?或者也许这要求过高了。我已经进行了一些编辑以提高清晰度。 - samvoit4
1个回答

3

概览

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

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

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

Crappy screenshot

# 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 #

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接