带错误栏的分组图

时间:2017-03-01 18:42:54

标签: r plot ggplot2 errorbar

我需要使用ggplot2 enter image description here

在R中重现此图

有谁知道怎么做? 谢谢。

1 个答案:

答案 0 :(得分:1)

假设您正在处理错误条的均值和标准偏差,您可以执行类似的操作(我创建了一个我认为可能看起来像您的数据框):

library(tidyverse)
df <- data.frame(num = runif(160, min = 0, max = 1),
             class = c(rep(c(rep("t-logistic", 4), rep("NSC", 4), 
                             rep("Poisson", 4), rep("PCC", 4), rep("Sparse PCC", 4),
                             rep("Sup. PCC", 4), rep("Sparse PLS", 4), rep("SVM", 4)), 5)),
             cat = c(rep(c("A", "B", "C", "D"), 40)))


df1 <- df %>%
  group_by(class, cat) %>%
  summarize(mean = mean(num), sd = sd(num))


ggplot(df1, aes(class, mean, color = cat))+
  geom_point(position = position_dodge(width = 0.6))+
  geom_errorbar(aes(class, ymin = (mean - 2*sd), ymax = (mean + 2*sd)), 
                position = position_dodge(width = 0.6))+
  theme_bw()

将为您提供此图表: enter image description here