累积定制的ggpairs()将对象绘制到列表对象中

时间:2018-03-24 22:26:57

标签: r ggplot2 ggally

我正在尝试创建一个包含GGally图的列表对象。这些图每个都使用两个数据集创建,主数据集和主要数据集的子集将再次以橙色绘制。在下面的MWE中,创建了三个图,每个图比较了mtcars数据中的两列,每个列包含不同数量的子集点,以橙色绘制:

Plot_1:mpg和cyl,1个橙色重叠点

Plot_2:mpg和disp,20个橙色重叠点

Plot_3:mpg和hp,30个橙色重叠点

library(GGally)
library(ggplot2)

data = mtcars
data$ID = rownames(mtcars)
data = data[, c(12,1:11)]

  my_fn <- function(data, mapping, ...){
    xChar = as.character(mapping$x)
    yChar = as.character(mapping$y)
    x = data[,c(xChar)]
    y = data[,c(yChar)]
    p <- ggplot(data, aes(x=x, y=y)) + geom_point() + geom_point(data = colorData, aes_string(x=xChar, y=yChar), inherit.aes = FALSE)
    p
  }

  ret=list()
  colorVec = c(1, 10, 20)
  k=1
    for (j in c(3:5)){
      datSel <- cbind(ID=data$ID, data[,c(2, j)])
      datSel$ID = as.character(datSel$ID)
      colorData <- datSel[sample(1:nrow(data), colorVec[k]),]
      p <- ggpairs(datSel[,-1], lower = list(continuous = my_fn), upper = list(continuous = wrap("cor", size = 4))) + theme_gray()
      ret[[paste0("Plot_",j)]] <- p
      k=k+1
    }  

但是,当我运行此代码并创建ret列表对象时,只有列表中的最后一个绘图对象才能成功创建绘图。前两个列表对象找不到数据中的一列。

> ret[["Plot_1"]]
Error in FUN(X[[i]], ...) : object 'cyl' not found

> ret[["Plot_2"]]
Error in FUN(X[[i]], ...) : object 'disp' not found

> ret[["Plot_3"]]
Correctly plotted

解决此问题可能是一种无痛的方式?提前感谢您分享建议。

编辑:

为再现性添加会话信息

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1 GGally_1.3.2 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15       reshape_0.8.7      grid_3.4.3         plyr_1.8.4         gtable_0.2.0      
 [6] magrittr_1.5       scales_0.5.0       pillar_1.2.1       stringi_1.1.6      rlang_0.2.0       
[11] reshape2_1.4.3     lazyeval_0.2.1     labeling_0.3       RColorBrewer_1.1-2 tools_3.4.3       
[16] stringr_1.3.0      munsell_0.4.3      yaml_2.1.17        compiler_3.4.3     colorspace_1.3-2  
[21] tibble_1.4.2

1 个答案:

答案 0 :(得分:2)

一个可能的解决方案,如果我正确理解你的问题:

library(GGally)
data = mtcars
data$ID = rownames(mtcars)
data = data[, c(12,1:11)]

# Load tidyverse
library(tidyverse)

# Create a vector list for each plot you want
var_list <- data.frame(var = names(data)[3:5], 
                   color = colorVec)

# Function for sampling orange points
my_color_fn <- function(data, color_nb) {
  sample(1:nrow(data), color_nb)
}

# Create a list with a data for each variable with colors
data_list <- apply(var_list, 1, 
                   function(x) 
                     data %>% 
                      select(ID, mpg, as.character(x[["var"]])) %>% 
                      mutate(color = "black") %>% 
                      mutate(color = replace(color, my_color_fn(., x[["color"]]), "orange")))

# Update my_fn function
my_fn <- function(data, mapping, ...){
  xChar = as.character(mapping$x)
  yChar = as.character(mapping$y)
  x = data[, c(xChar)]
  y = data[, c(yChar)]
  p <- ggplot(data, aes_string(x=x, y=y)) + 
    geom_point(aes(color = color)) + 
    scale_color_manual("", values = c("black" = "black",
                                      "orange" = "orange"))
  p
}

# Create a function to get ggpairs for each subset
my_fn2 <- function(data)
{
  p <- ggpairs(data %>% select(- ID), 1:2, 
               lower = list(continuous = my_fn), 
               upper = list(continuous = wrap("cor", size = 4)))
  return(p)
}

# Get plot for each list element
ret <- lapply(data_list, function(x) my_fn2(x))

ret[[1]]
ret[[2]]
ret[[3]]

plot_1 plot_2 plot_3