使用dplyr创建多个ggplots

时间:2018-09-20 23:29:11

标签: r ggplot2 ggally

使用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 个答案:

答案 0 :(得分:2)

概述

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

然后,我创建了另一个自定义函数ManyScatterPlots()-使用purrr::map()-为x轴上df中每个特定列和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 #