gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:33:22 27 4
gpt4 key购买 nike

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

Plot_1:mpg 和 cyl,1 个橙色覆盖点

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

Plot_3:英里/加仑和马力,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

什么是解决此问题的无痛方法?预先感谢您分享建议。

编辑:

添加 session 信息以提高重现性

> 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

最佳答案

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

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

关于r - 将定制的 ggpairs() 绘图对象累积到列表对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com