R 中的系数图;更改 CI 行颜色
coefplot in R; change CI line colours
您好,我在 r 中使用 coefplot 函数绘制广义线性模型的系数。我想将 95% CI 线的颜色更改为不同于 50% CI 线的颜色。对于 95% 和 50% CI 行,颜色参数默认使用相同的颜色。
coeff<-coefplot(model1,pointSize=5,color="black",fillColor="grey",lwdOuter = 1.2,lwdInner=2)
coeff + theme_bw() +
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank()) +
theme (axis.title.y = element_text(size=16)) +
theme(axis.title.x = element_text(size=16)) +
scale_y_discrete(name="",labels=c("NDAA","GAP","SS","PS","LL")) +
theme (axis.text.x = element_text(size=16)) +
theme(axis.text.x = element_text(size=16)) +
scale_x_continuous(name="Regression Estimate") +
labs(title = "") +
coord_flip()
这里不能轻易改变颜色。
不幸的是,该软件包不允许在此处设置颜色。
- 您可以使用
grid
包或 gTable
更改 grobs 颜色。这是肮脏的解决方案。它假设您知道一点如何在 ggplot 树对象 (gpath) 中导航
- 或者您向 buildPlotting.lm 添加一个新的颜色参数。你应该重新编译这个包。
您或许可以创建自己的系数图版本来满足您的需要,而不会遇到太多麻烦。这是一个 ggplot2
示例:
library(ggplot2)
# Create a model to plot
m1 = lm(mpg ~ wt + cyl + carb, data=mtcars)
coefs = as.data.frame(summary(m1)$coefficients[-1,1:2])
names(coefs)[2] = "se"
coefs$vars = rownames(coefs)
ggplot(coefs, aes(vars, Estimate)) +
geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
geom_errorbar(aes(ymin=Estimate - 1.96*se, ymax=Estimate + 1.96*se),
lwd=1, colour="red", width=0) +
geom_errorbar(aes(ymin=Estimate - se, ymax=Estimate + se),
lwd=2.5, colour="blue", width=0) +
geom_point(size=4, pch=21, fill="yellow") +
theme_bw()
我遇到了同样的问题。
如果您更改单个 ggplot2
层,则可以在 coefplot
内解决它。
coef <- coefplot(model = model1
, color = "blue"
) +
theme_bw()
coef$layers[[2]] <- geom_errorbarh(stat="identity", position = "identity", na.rm = FALSE
, color = "yellow"
, size = 1
, mapping = aes(xmin = LowOuter, xmax = HighOuter
, height = 0, linetype = Model
))
coef$layers[[3]] <- geom_errorbarh(stat="identity", position = "identity", na.rm = FALSE
, color = "red"
, size = 2
, mapping = aes(xmin = LowInner, xmax = HighInner
, height = 0, linetype = Model
))
coef
coefplot image
您好,我在 r 中使用 coefplot 函数绘制广义线性模型的系数。我想将 95% CI 线的颜色更改为不同于 50% CI 线的颜色。对于 95% 和 50% CI 行,颜色参数默认使用相同的颜色。
coeff<-coefplot(model1,pointSize=5,color="black",fillColor="grey",lwdOuter = 1.2,lwdInner=2)
coeff + theme_bw() +
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank()) +
theme (axis.title.y = element_text(size=16)) +
theme(axis.title.x = element_text(size=16)) +
scale_y_discrete(name="",labels=c("NDAA","GAP","SS","PS","LL")) +
theme (axis.text.x = element_text(size=16)) +
theme(axis.text.x = element_text(size=16)) +
scale_x_continuous(name="Regression Estimate") +
labs(title = "") +
coord_flip()
这里不能轻易改变颜色。 不幸的是,该软件包不允许在此处设置颜色。
- 您可以使用
grid
包或gTable
更改 grobs 颜色。这是肮脏的解决方案。它假设您知道一点如何在 ggplot 树对象 (gpath) 中导航
- 或者您向 buildPlotting.lm 添加一个新的颜色参数。你应该重新编译这个包。
您或许可以创建自己的系数图版本来满足您的需要,而不会遇到太多麻烦。这是一个 ggplot2
示例:
library(ggplot2)
# Create a model to plot
m1 = lm(mpg ~ wt + cyl + carb, data=mtcars)
coefs = as.data.frame(summary(m1)$coefficients[-1,1:2])
names(coefs)[2] = "se"
coefs$vars = rownames(coefs)
ggplot(coefs, aes(vars, Estimate)) +
geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
geom_errorbar(aes(ymin=Estimate - 1.96*se, ymax=Estimate + 1.96*se),
lwd=1, colour="red", width=0) +
geom_errorbar(aes(ymin=Estimate - se, ymax=Estimate + se),
lwd=2.5, colour="blue", width=0) +
geom_point(size=4, pch=21, fill="yellow") +
theme_bw()
我遇到了同样的问题。
如果您更改单个 ggplot2
层,则可以在 coefplot
内解决它。
coef <- coefplot(model = model1
, color = "blue"
) +
theme_bw()
coef$layers[[2]] <- geom_errorbarh(stat="identity", position = "identity", na.rm = FALSE
, color = "yellow"
, size = 1
, mapping = aes(xmin = LowOuter, xmax = HighOuter
, height = 0, linetype = Model
))
coef$layers[[3]] <- geom_errorbarh(stat="identity", position = "identity", na.rm = FALSE
, color = "red"
, size = 2
, mapping = aes(xmin = LowInner, xmax = HighInner
, height = 0, linetype = Model
))
coef
coefplot image