r - 在 rstanarm/bayesplot 图中设置单个颜色,而不是配色方案
问题描述
我想知道是否可以将颜色直接应用于带有bayesplot的rstanarm模型的参数
例如来自https://mc-stan.org/bayesplot/articles/plotting-mcmc-draws.html:
library("bayesplot")
library("ggplot2")
library("rstanarm")
fit <- stan_glm(mpg ~ ., data = mtcars, seed = 1111)
posterior <- as.array(fit)
color_scheme_set("red")
mcmc_intervals(posterior, pars = c("cyl", "drat", "am", "sigma"))
返回:
该文档描述了自定义方案或混合,但它们仍然适用于图中的每个参数:
color_scheme_set("mix-blue-red")
plot(fit, pars = c("cyl", "drat", "am", "sigma"))
plot 函数(带有 rstanarm 模型)不再接受col
能够指定每个点的参数。无论如何为图中的每个参数指定一串颜色(或方案)?
编辑:根据@Limey 的评论,您可以将绘图修改为 ggobjects,但我没有看到一种直接访问原始绘图美学的方法。
例如,scale_colour_manual
(或任何其他scale_colour_...
)不会改变绘制的值:
plot(fit, pars = c("cyl", "drat", "am", "sigma"))+
scale_colour_manual(values=c("blue", "purple", "orange","red"))
(见上图,看起来一样)。另一个例子,我可以在原图之上绘制点:
plot(fit, pars = c(names(mtcars)[-1]))+
geom_point(aes(x=fit$coefficients[-1],y=names(fit$coefficients)[-1]),color="black")
并使它们更大以匹配(注意我在这里也将它们设为粉红色):
plot(fit, pars = c(names(mtcars)[-1]))+
geom_point(aes(x=fit$coefficients[-1],y=names(fit$coefficients)[-1]),color="pink",size=3)
我也可以对每个误差条对象执行此操作,但此时我并没有真正修改原始图,而是在它的顶部绘制。在这种情况下,将 rstanarm 绘图函数全部丢弃并简单地手动执行,或者创建我自己的函数是有意义的,这很好。但是,如果有人知道如何plot(rstanarm_object)
直接修改颜色,我仍然有兴趣听听。
解决方案
这并不能直接解决问题,但如果没有其他解决方案出现,它可能仍然是一种可以改进的解决方法。
通过@Limey 的评论源于对 OP 的编辑:
您可以自己从模型对象中绘制所有内容,该对象接近绘制的值。我意识到这并不完美,也许有人对这些值如何完美重叠提出了建议。rstanarm
绘图默认为 50% 和 90%的可信区间。我在这里使用了 t 表和 50% 和 90%的置信区间,它们并不相同,但结果很接近。
color_scheme_set("gray")
vars<-c(2,5,9,11) # select values from fit$coefficients
# (this is assumed to match with names(mtcars))
# Note I have switched `sigma` to `carb` for ease of model retrieval
plot(fit, pars = c(names(mtcars)[vars]))+ # the original plot, now in grey
# the first error bar. With a t table, 1.645 is a 90% confidence interval,
# which isn't the same here as the 90% credible interval from rstanarm, but it
# gets us close
geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*1.645),
xmax=(fit$coefficients[vars]+fit$ses[vars]*1.645),
y=names(fit$coefficients)[vars]),
height=0,alpha=0.5,color="yellow" # transparency is so you can see grey through the yellow.. I know this isn't pretty
)+
geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*.675), # this is .675, which corresponds to a 50% confidence interval (again not the same as a credible interval, but it is getting us close
xmax=(fit$coefficients[vars]+fit$ses[vars]*.675),
y=names(fit$coefficients)[vars]),
height=0,lwd=2,alpha=0.5,color="yellow"
)+
# now plot the points over the lines:
geom_point(aes(x=fit$coefficients[vars],
y=names(fit$coefficients)[vars]),
color="yellow",size=3,alpha=0.5)
这不漂亮,也不准确(你可以看到黄色边缘有点灰色),但它很接近。如果您可以修改每个值集的颜色,那么您可以交替使用颜色:
colors=c("red","blue","red","blue") # selecting colors one by one
colors=rep(c("red","blue"),2) # or just have alternating colors repeat (2 times here)
ggplot()+ # note that I have removed `rstanarm` object, which is no longer necessary
geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*1.645),
xmax=(fit$coefficients[vars]+fit$ses[vars]*1.645),
y=names(fit$coefficients)[vars]),
height=0,alpha=0.5,color=colors
)+
geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*.675),
xmax=(fit$coefficients[vars]+fit$ses[vars]*.675),
y=names(fit$coefficients)[vars]),
height=0,lwd=2,alpha=0.5,color=colors
)+
geom_point(aes(x=fit$coefficients[vars],
y=names(fit$coefficients)[vars]),
color=colors,size=3,alpha=0.5)
或完全古怪的东西:
colors=c("red","blue","green","yellow")
colors2=rev(colors)
ggplot()+
geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*1.645),
xmax=(fit$coefficients[vars]+fit$ses[vars]*1.645),
y=names(fit$coefficients)[vars]),
height=0,alpha=0.5,color=colors
)+
geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*.675),
xmax=(fit$coefficients[vars]+fit$ses[vars]*.675),
y=names(fit$coefficients)[vars]),
height=0,lwd=2,alpha=0.5,color=colors
)+
geom_point(aes(x=fit$coefficients[vars],
y=names(fit$coefficients)[vars]),
color=colors2,size=3,alpha=0.5)
推荐阅读
- java - WebSphere 和 Tomcat 之间的负载平衡
- javascript - 为什么这在 IE 和 Chrome 中都不起作用。表格似乎只在 Chrome 中正确显示?
- hadoop - 无法以非 sudo 用户身份写入 HDFS
- servicenow - ServiceNow 中的筛选报告筛选器
- function - 为什么 c++11 将 get<>(tuple) 定义为全局函数而不是元组的成员?
- c - 我如何检查结构是否包含任何数据,因为我正在循环传递它?
- smooch - 无法将用户链接到 Whatsapp 频道
- rest - 哪个 REST-API URL 用于将属性从一个实体复制到另一个实体?
- css - css过滤器来获得图像“蓝调”?
- javascript - 排除来自身份验证快速站点的路由