使用dplyr创建多个ggplots [英] Creating multiple ggplots with dplyr

查看:48
本文介绍了使用dplyr创建多个ggplots的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 GGally :: ggpairs()创建了一个绘图矩阵之后,我想存储各个散点图以备后用.

这是我当前的代码:

 #加载必要的程序包library(GGally)#加载ggplot2library(magrittr)#允许使用`%>%`#创建一个绘图矩阵mtcars%>%na.omit()%>%ggpairs(列= 1:7)#如何自动执行此过程?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()管道中指定数据.

任何建议都值得赞赏!

解决方案

概述

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

然后我创建了另一个自定义函数 ManyScatterPlots()-使用

 #加载必要的程序包-----图书馆(tidyverse)#创建一个可以绘制散点图的函数ScatterPlot<-function(df,x,y){# 输入:#df:数据框#x:来自df的字符矢量形式的列#y:df中字符向量形式的列## 输出:#ggplot2图要求(ggplot2)ggplot(数据= df,aes(x = get(x),y​​ = get(y)))+geom_point()+xlab(x)+ylab(y)+labs(title = paste0(y,如,x所解释"))}#创建一个为每种可能的列组合绘制一个ScatterPlot()的函数-------ManyScatterPlots<-function(df){# 输入:#df:数据框## 输出:#ScatterPlot()的ggplot2图的列表要求(magrittr)要求(purrr)#df中的每一列#在x轴上为该列创建一个单独的散点图#和y轴上的每一列colnames(df)%&%;%map(.f = function(i)map(.x = colnames(df),.f = function(j)ScatterPlot(df = df,x = i,y = j))%>%#以帮助识别该特定列的各个图#在列表中标记地块purrr :: set_names(nm = paste0(colnames(df),如,i)))%&%;%#帮助识别每个特定列的绘图列表#在列表中标记地块purrr :: set_names(nm = colnames(df))}#使用ManyScatterPlots()-----many.plots<-ManyScatterPlots(df = mtcars)# 查看结果  - -names(many.plots)#列表列表map(.x = many.plots,names)#单个散点图的列表如DISP所解释的many.plots $ disp $ hp如drat所述的many.plots $ drat $ hphp解释的many.plots $ hp $ qsec#脚本结尾# 

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!

解决方案

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 #

这篇关于使用dplyr创建多个ggplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆