-1

我正在尝试从 R 中的 Stata 复制图表。我有几个变量,并希望在每个有两个的治疗组中显示它们的平均值。Stata图如下:

coefplot 试图模仿

该系数图实际上不是系数图,而是每个单独变量的每个处理的平均值。df 基本上看起来像。

可操作的数据

4

2 回答 2

1

如果没有可重复的数据,很难回答您的问题。


但是,这可能会得到你想要的意思:

library(dplyr)
mpg %>% 
  select(manufacturer, cty, trans) %>% 
  group_by(manufacturer, trans) %>% 
  summarize(cty_mean = mean(cty)) %>% 
  ggplot(aes(x=cty_mean, y=reorder(manufacturer, cty_mean), color=trans)) +
  geom_point() 

在此处输入图像描述

如果您还希望包含系数或标准错误,那么您可以通过在summarize().

于 2018-07-19T12:34:37.113 回答
0

我发现 geom_pointrange() 可能是您正在寻找的:

library("ggplot2")
set.seed(111018)
interval1 <- -qnorm((1-0.9)/2)  

means_treatment_1 <- rnorm(2)
se_treatment_1 <- rnorm(2)

df_treatment_1 <- data.frame("Mean" = means_treatment_1,
                         "lower" = means_treatment_1 - se_treatment_1*interval1,
                         "upper" = means_treatment_1 + se_treatment_1*interval1,
                         "Variable" = c("medicare_spending_dummy", 
                                        "job_training_dummy"),
                         "Treatment" = "a")


means_treatment_2 <- rnorm(2)
se_treatment_2 <- rnorm(2)

df_treatment_2 <- data.frame("Mean" = means_treatment_2,
                         "lower" = means_treatment_2 - se_treatment_2*interval1,
                         "upper" = means_treatment_2 + se_treatment_2*interval1,
                         "Variable" = c("medicare_spending_dummy", 
                                        "job_training_dummy"),
                         "Treatment" = "b")



df_tot<-rbind(df_treatment_1, df_treatment_2)



# Plot


ggplot(df_tot, aes(colour = Treatment)) +
geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) +
geom_pointrange(aes(x = Variable, y = Mean, ymin = lower, ymax = upper ),lwd = 1, position = position_dodge(width = 1/2)) +

coord_flip() + 
theme_bw()

在此处输入图像描述

于 2018-07-19T16:22:13.097 回答