pac*_*ese 5 plot r ggplot2 ggally
我看到这些帖子 GGally :: ggpairs绘图没有网格线时绘制相关系数 使用ggpairs来创建这个图
阅读后,我能够实现这个黑客https://github.com/tonytonov/ggally/blob/master/R/gg-plots.r,我的情节看起来像这样
我认为这是一个很好的结果,但我无法改变颜色.
MWE就是这个
library(ggally)
# load the hack
source("ggally_mod.R")
# I saved https://github.com/tonytonov/ggally/blob/master/R/gg-plots.r as "ggally_mod.R"
assignInNamespace("ggally_cor", ggally_cor, "GGally")
ggpairs(swiss)
Run Code Online (Sandbox Code Playgroud)
现在我想跑
ggpairs(swiss,
lower=list(continuous="smooth", wrap=c(colour="blue")),
diag=list(continuous="bar", wrap=c(colour="blue")))
Run Code Online (Sandbox Code Playgroud)
但颜色保持不变.有没有办法改变颜色,因为params不再工作了?
您没有wrap正确使用-
请参阅插图以获取详细信息.另外对于对角线你现在必须使用该功能barDiag(但是ggpairs给出非常有用的错误来告诉这个)
因此,对于您的示例,我们可以更改colour下面板和fill下面条形图中的点
library(GGally)
library(ggplot2)
ggpairs(swiss[1:3],
lower=list(continuous=wrap("smooth", colour="blue")),
diag=list(continuous=wrap("barDiag", fill="blue")))
Run Code Online (Sandbox Code Playgroud)
但是,由于光滑的颜色是硬编码的(请参阅参考资料ggally_smooth),要更改其颜色,您需要定义自己要传递的函数.所以从这里开始
my_fn <- function(data, mapping, pts=list(), smt=list(), ...){
ggplot(data = data, mapping = mapping, ...) +
do.call(geom_point, pts) +
do.call(geom_smooth, smt)
}
# Plot
ggpairs(swiss[1:4],
lower = list(continuous =
wrap(my_fn,
pts=list(size=2, colour="red"),
smt=list(method="lm", se=F, size=5, colour="blue"))),
diag=list(continuous=wrap("barDiag", fill="blue")))
Run Code Online (Sandbox Code Playgroud)
以类似的方式,这里有一种定义新的上相关函数的方法(类似于你所拥有的)
cor_fun <- function(data, mapping, method="pearson", ndp=2, sz=5, stars=TRUE, ...){
x <- eval_data_col(data, mapping$x)
y <- eval_data_col(data, mapping$y)
corr <- cor.test(x, y, method=method)
est <- corr$estimate
lb.size <- sz* abs(est)
if(stars){
stars <- c("***", "**", "*", "")[findInterval(corr$p.value, c(0, 0.001, 0.01, 0.05, 1))]
lbl <- paste0(round(est, ndp), stars)
}else{
lbl <- round(est, ndp)
}
ggplot(data=data, mapping=mapping) +
annotate("text", x=mean(x, na.rm=TRUE), y=mean(y, na.rm=TRUE), label=lbl, size=lb.size,...)+
theme(panel.grid = element_blank())
}
ggpairs(swiss,
lower=list(continuous=wrap("smooth", colour="blue")),
diag=list(continuous=wrap("barDiag", fill="blue")),
upper=list(continuous=cor_fun))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3910 次 |
| 最近记录: |