gpt4 book ai didi

r - 使用 ggpairs 创建此图

转载 作者:行者123 更新时间:2023-12-03 23:47:52 32 4
gpt4 key购买 nike

我在 Shiny 应用程序中有一些代码可以生成下面的第一个图。如您所见,字体大小随相关系数的大小而变化。我想用 ggpairs (GGally) 或 ggplot2 制作类似的东西。下面的第二张图片是用以下代码生成的:

library(GGally)
ggpairs(df,
upper = list(params = c(size = 10)),
lower = list(continuous = "smooth", params = c(method = "loess", fill = "blue"))
)

如您所见,相关字体的大小可以使用 size 进行调整,但是当我设置大小向量时,仅使用第一个值。我还想删除“Corr:”并添加一个重要指标。使用颜色作为相关系数的符号也会很好。在下, methodfill未链接到 smooth .关于如何获得第二个情节以捕捉第一个情节的更多特征的任何建议都会很棒。

Anscombe's data :
df <- structure(list(y1 = c(8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 
4.26, 10.84, 4.82, 5.68), x1 = c(10L, 8L, 13L, 9L, 11L, 14L,
6L, 4L, 12L, 7L, 5L), y2 = c(9.14, 8.14, 8.74, 8.77, 9.26, 8.1,
6.13, 3.1, 9.13, 7.26, 4.74), x2 = c(10L, 8L, 13L, 9L, 11L, 14L,
6L, 4L, 12L, 7L, 5L), y3 = c(7.46, 6.77, 12.74, 7.11, 7.81, 8.84,
6.08, 5.39, 8.15, 6.42, 5.73), x3 = c(10L, 8L, 13L, 9L, 11L,
14L, 6L, 4L, 12L, 7L, 5L)), .Names = c("y1", "x1", "y2", "x2",
"y3", "x3"), class = "data.frame", row.names = c(NA, -11L))

correlation plot using pairs
# based mostly on http://gallery.r-enthusiasts.com/RGraphGallery.php?graph=137
panel.plot <- function(x, y) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
ct <- cor.test(x,y)
sig <- symnum(ct$p.value, corr = FALSE, na = FALSE,
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", ".", " "))
r <- ct$estimate
rt <- format(r, digits=2)[1]
cex <- 0.5/strwidth(rt)

text(.5, .5, rt, cex=cex * abs(r))
text(.8, .8, sig, cex=cex, col='blue')
}
panel.smooth <- function (x, y) {
points(x, y)
abline(lm(y~x), col="red")
lines(stats::lowess(y~x), col="blue")
}
pairs(df, lower.panel=panel.smooth, upper.panel=panel.plot)

correlation plot using ggpairs

最佳答案

为 GGally 1.0.1 编辑
params现在已弃用,请使用 wrap像这样:

ggpairs(df[, 1:2], 
upper = list(continuous = wrap("cor", size = 10)),
lower = list(continuous = "smooth"))
enter image description here
原答案
通过参数列表并不总是可以定制复杂的图。这是很自然的:要记住的参数太多了。所以唯一可靠的选择是修改源。当项目是 hosted on github时,这一点尤其令人愉快。 .
这是一个简单的修改开始,在 forked repo 中进行.更新代码并生成下图的最简单方法是复制并粘贴函数 ggally_cor到您的全局环境,然后覆盖 GGally 中的相同功能命名空间:
# ggally_cor <- <...>
assignInNamespace("ggally_cor", ggally_cor, "GGally")
ggpairs(df[, 1:2],
upper = list(params = c(size = 10)),
lower = list(continuous = "smooth"))
enter image description here
我删除了文本标签并添加了重要性指标。但是,修改颜色和大小并不是那么容易,因为这些是更早映射的。我仍在考虑它,但您明白了,可以继续进行进一步的自定义。
编辑:我已经更新了代码,请参阅我的最新提交。现在它将标签的大小映射到相关性的绝对值。如果你想要不同的颜色,你可以做类似的事情,虽然我认为这可能不是一个很好的主意。
enter image description here

关于r - 使用 ggpairs 创建此图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21716567/

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