Creating multiple ggplots with dplyr

China☆狼群 提交于 2021-02-19 23:08:14

问题


After creating a plot matrix using GGally::ggpairs(), I would like to store the individual scatter plots for later use.

Here is my current code:

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

I'm getting an error that says the data must be a dataframe. I tried to specify the data from the na.omit() pipe using a . but I received the same result.

Any advice is appreciated!


回答1:


Overview

I condensed all the separate ggplot(...) calls into one custom function: ScatterPlot().

Then I created another custom function ManyScatterPlots() - which uses purrr::map() - that stores the individual scatter plot for each particular column in df on the x-axis and every column on the y-axis in a list. This process repeats itself for every column in df.

The result from ManyScatterPlots() is a list of lists, where each individual list contains many scatter plots. I've labeled both the list of lists and the individual plots to make it easier to find what you're looking for later on.

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


来源:https://stackoverflow.com/questions/52434628/creating-multiple-ggplots-with-dplyr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!