在 R 中使用 GGally 修正多组密度图的比例
Correct scale of density plots with multiple groups using GGally in R
我正在尝试使用 R 中 GGally 库中的 ggpairs 可视化数据集。我想要一个对角线,每个变量的密度图由一个分组变量分隔。由于规模问题,我无法获得正确的地块。为了说明我的观点,我将使用以下人工数据集:
group=as.numeric(cut(runif(100),c(0,1/2,1),c(1,2)))
x=rnorm(100,group,1)
x[group==1]=(x[group==1])^2
y=2*x+rnorm(100,0,0.1)
data=data.frame(group=as.factor(group),x=x,y=y)
使用ggpairs,我得到以下图
library(ggplot2)
library(GGally)
ggpairs(data,columns = 2:3,colour="group")
现在,将左上图与使用普通 ggplot2 获得的变量 x 的密度图进行比较:
ggplot(data, aes(x = x, colour = group)) + geom_density()
我们可以看到ggpairs(第一张图)中红蓝曲线的y scale不一样,这可能会导致错误的结论。我如何在 ggpairs 中更正此问题?
开发者的回答如下:
You are correct. they are not displaying correctly. :-(
With the current CRAN release, please try the following...
set.seed(1234)
group = as.numeric(cut(runif(100),c(0,1/2,1),c(1,2)))
x = rnorm(100,group,1)
x[group == 1] = (x[group == 1])^2
y = (2 * x) + rnorm(100,0,0.1)
data = data.frame(group = as.factor(group), x = x, y = y)
library(ggplot2)
library(GGally)
# # bad example
# ggpairs(data,columns = 2:3,colour="group")
ggally_correct_diag_densityDiag <- function(data, mapping, ...) {
# the color is corrected to fill by ggpairs
# to get desired output with color, it is changed back here.
if (! is.null(mapping$fill)) {
mapping$colour = mapping$fill
mapping$fill = NULL
}
ggplot(data, mapping) + geom_density(...)
}
ggpairs(data, columns = 2:3, colour = "group", diag = list(continuous = "correct_diag_density"))
Until the next release, you can leverage the eval process of ggpairs.
"ggally_FN_NAME" or "ggally_FN_NAMEDiag" are the naming conventions to
follow. The next release will allow for submission of custom functions
directly such as:
ggpairs(data, columns = 2:3, colour = "group", diag = list(continuous = ggally_correct_diag_densityDiag))
在他们的 github page
中查看更多详细信息
我正在尝试使用 R 中 GGally 库中的 ggpairs 可视化数据集。我想要一个对角线,每个变量的密度图由一个分组变量分隔。由于规模问题,我无法获得正确的地块。为了说明我的观点,我将使用以下人工数据集:
group=as.numeric(cut(runif(100),c(0,1/2,1),c(1,2)))
x=rnorm(100,group,1)
x[group==1]=(x[group==1])^2
y=2*x+rnorm(100,0,0.1)
data=data.frame(group=as.factor(group),x=x,y=y)
使用ggpairs,我得到以下图
library(ggplot2)
library(GGally)
ggpairs(data,columns = 2:3,colour="group")
现在,将左上图与使用普通 ggplot2 获得的变量 x 的密度图进行比较:
ggplot(data, aes(x = x, colour = group)) + geom_density()
我们可以看到ggpairs(第一张图)中红蓝曲线的y scale不一样,这可能会导致错误的结论。我如何在 ggpairs 中更正此问题?
开发者的回答如下:
You are correct. they are not displaying correctly. :-(
With the current CRAN release, please try the following...
set.seed(1234) group = as.numeric(cut(runif(100),c(0,1/2,1),c(1,2))) x = rnorm(100,group,1) x[group == 1] = (x[group == 1])^2 y = (2 * x) + rnorm(100,0,0.1) data = data.frame(group = as.factor(group), x = x, y = y) library(ggplot2) library(GGally) # # bad example # ggpairs(data,columns = 2:3,colour="group") ggally_correct_diag_densityDiag <- function(data, mapping, ...) { # the color is corrected to fill by ggpairs # to get desired output with color, it is changed back here. if (! is.null(mapping$fill)) { mapping$colour = mapping$fill mapping$fill = NULL } ggplot(data, mapping) + geom_density(...) } ggpairs(data, columns = 2:3, colour = "group", diag = list(continuous = "correct_diag_density"))
Until the next release, you can leverage the eval process of ggpairs. "ggally_FN_NAME" or "ggally_FN_NAMEDiag" are the naming conventions to follow. The next release will allow for submission of custom functions directly such as:
ggpairs(data, columns = 2:3, colour = "group", diag = list(continuous = ggally_correct_diag_densityDiag))
在他们的 github page
中查看更多详细信息