【问题标题】:How to plot points with error bars per group in ggplot?如何在ggplot中绘制每组带有误差线的点?
【发布时间】:2020-11-27 13:47:28
【问题描述】:

我有以下数据框:


mlt = structure(list(V1 = c("Female > 55", "Male > 55", "Finance Industry", 
"South"), climatechange = c(0.116, 0.093, -0.186, 0.311), `SE climatechange` = c(0.299720594648884, 
0.209685397712576, 0.198822469025311, 0.220794746780239), housing = c(-0.223, 
0.288, -0.063, -0.401), `SE housing` = c(0.240842826106496, 0.262613125585529, 
0.233796203318591, 0.112279926339202), macro = c(-0.219, -0.243, 
0.252, 0.058), `SE macro` = c(0.167027449787956, 0.109527362813708, 
0.225740740488032, 0.139837005156927), `pension and savings` = c(0.287, 
0.815, 0.348, -0.611), `SE pension and savings` = c(0.278129111903788, 
0.26508680711124, 0.250889015745089, 0.0571662145564419)), row.names = c(NA, 
-4L), class = "data.frame")

View(df)

数据框有 4 个主题,每个主题都有相应的标准错误。到目前为止,我只绘制了第一个主题:

mlt1 = mlt[,c(1:3)]

ggplot(mlt1, aes(V1, climatechange,
                 ymin = climatechange - 1.96*`SE climatechange`, ymax= climatechange + 1.96*`SE climatechange`)) +
  scale_x_discrete('') +
  scale_y_continuous('Marginal Effect \n',limits=c(-1,1)) +
  theme_classic() +
  theme(panel.border = element_rect(fill=NA)) + geom_errorbar(aes(x = V1, y = climatechange),
                                                              size=0.8,width=.2, col = "#0072B2") + 
  geom_point(aes(fill=factor(climatechange)), size=5, shape = 21) +
  scale_fill_manual(values=c(rep("#0072B2",13))) + 
  geom_hline(yintercept=0, col = "grey")  + theme(legend.position="none") + theme(axis.text = element_text(color = "black", size = 12),
                                                       axis.title = element_text(size = 12)) + labs(title="Climate Change") +
  theme(plot.title = element_text(color = "black", size = 12, face = "bold", hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5))

不过,我想做的是得到一个看起来像这样的图表:

where 而不是 A,B,... 我有四个主题,每个主题都显示相应的变量。 环顾四周,我看到有人使用:facet_grid(.~Test, space = "free_x", shrink = T, scales = "free_x")。然而;我不确定如何应用到我的数据集。

谁能帮帮我?

谢谢!

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    要对所有四个进行操作 - 首先您需要旋转几次以使数据框整洁(即为 measure(主题)、meanmeanSE 创建一个列),然后创建数据框分面通过度量变量:

    mlt %>% 
      pivot_longer(2:9, 
                   names_to = c("measure"), 
                   values_to = "est") %>%
      mutate(stat = ifelse(str_detect(measure, "^SE"), "SE", "mean"),
             measure = str_extract(measure, "\\w*$")) %>% 
      pivot_wider(names_from = "stat",
                  values_from = "est", 
                  id_col = c(V1, measure)) %>% 
      ggplot(aes(x = V1, mean,
                 ymin = mean - 1.96*SE,
                 ymax = mean + 1.96*SE)) +
      scale_x_discrete('') +
      scale_y_continuous('Marginal Effect \n', 
                         # limits=c(-1,1)
      ) +
      coord_cartesian(ylim = c(-1, 1)) +
      theme_classic() +
      theme(panel.border = element_rect(fill=NA)) + 
      geom_errorbar(size=0.8,width=.2, col = "#0072B2") + 
      geom_point(size=5, shape = 21) +
      scale_fill_manual(values=c(rep("#0072B2",13))) + 
      geom_hline(yintercept=0, col = "grey")  + 
      theme(legend.position="none") + 
      theme(axis.text = element_text(color = "black", size = 12),
            axis.title = element_text(size = 12)) + labs(title="Climate Change") +
      theme(plot.title = element_text(color = "black", size = 12, face = "bold", hjust = 0.5)) +
      theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5)) +
      facet_wrap(~measure, nrow = 1)
    
    

    【讨论】:

    • 差不多。我想你只需要翻转它。这些列应该是:气候变化、住房、宏观和养老金以及储蓄。然后,在每个组中,我将针对女性>55、男性>55、金融业、南方的分数和等级。如果你能做到这一点,我将永远感激不尽
    • 啊,我明白了!所以你的数据框的一点点转换应该让它变得简单。将在一秒钟内更新...
    • 希望对您有所帮助 - 如果更多说明如何工作的说明会有所帮助,请告诉我,但如果足够清楚,那么祝您顺利完成后续步骤!
    • 抱歉刚刚取出填充命令使它更整洁。取出 shape = 21 使其默认返回填充点,或在其后设置 fill = "blue" 或类似名称。
    • 应该也检查一下 - 您是否希望按主题更改填充?然后 fill = measure 里面的每个方面都会有不同的颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2021-04-20
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2017-02-16
    • 2021-07-09
    相关资源
    最近更新 更多