我有两组固定效果模型的分面网格图。在第一组中,我想抑制控制变量(显示为绿色)。在第二组中,我想显示控制变量,如果模型中有一个带有position_dodgev()
的变量。
到目前为止,我已经弄清楚了这段代码:
library(ggplot2)
library(ggstance)
ggplot(mf, aes(fill=mn)) +
geom_vline(xintercept=0) +
geom_pointrangeh(aes(y=mn, x=coef, group=cv, color=cv,
xmin=coef - se*1.96, xmax=coef + se*1.96),
position=position_dodgev(.5),
show.legend=FALSE) +
scale_color_manual(values=c("green", "red", "green", "red")) +
facet_grid(gr ~ ., scales="free", space="free")
这给了我这个:但是,在模型1中,解释变量是重复的,而组2的模型中的解释变量并不总是在最前面。
我实际上想看这样的情节(photoshopped):
ggplot()
怎么可能?数据
mf <- structure(list(coef = c(3.025, 0.762499999999999, -1.44237073106609,
-0.125042600081792, -0.689108910891089, 2.64264321029771, 2.64264321029771
), se = c(5.26319027539381, 3.34469904756018, 2.02098677878979,
2.02098677878979, 2.02098677878979, 0.763989041657158, 0.763989041657158
), mn = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 4L), .Label = c("Model 2c",
"Model 2b", "Model 2a", "Model 1"), class = "factor"), gr = c(2,
2, 2, 2, 2, 1, 1), cv = structure(c(2L, 1L, 2L, 3L, 2L, 2L, 3L
), .Label = c("gear", "vs", "disp"), class = "factor")), row.names = c("vs",
"gear", "vs1", "disp", "1", "11", "2"), class = "data.frame")
最佳答案
对数据进行一些小的更改将有助于修复该图。首先,我们可以删除模型1的重复行。其次,简历颜色更改顺序的问题是,模型2a和2b之间的控制变量不同。我们可以创建一个指标列以简单地说出该行是否为控制变量,然后使用该列为绘图着色。
library(tidyverse)
library(ggstance)
mf %>%
#remove the dupe from Model 1
filter(!(mn == "Model 1" & cv == "disp")) %>%
#create an indicator column for control variables
mutate(Control = cv == "vs") %>%
ggplot(aes(fill=mn)) +
geom_vline(xintercept=0) +
#group and color using our new indicator variable
geom_pointrangeh(aes(y=mn, x=coef, group=Control, color=Control,
xmin=coef - se*1.96, xmax=coef + se*1.96),
position=position_dodgev(.5),
show.legend=FALSE) +
scale_color_manual(values=c("green", "red", "green", "red")) +
facet_grid(gr ~ ., scales="free", space="free")