【问题标题】:how to do geom_error with long format如何用长格式做geom_error
【发布时间】:2016-10-18 18:55:34
【问题描述】:

我有这个长格式的数据框“dat”,该图使用折线图显示时间序列 a 和 b,带有上下误差线,但我想改用 geom_error()。当 dat 采用这种格式时,如何将绘图更改为使用 geom_error()?

问题是 geom_error 需要一个 ymin 和 ymax

 geom_errorbar(aes(ymax = Upper ,ymin = Lower), width = .25) 

但是当数据是长格式时,ymin 和 max 在 4 个时间序列中; upper_a、lower_a、upper_b 和 lower_b

dat = data.frame(x= c(1,2,1,2,1,2,1,2,1,2,1,2),y = c(1,2,5,6,2,3,0,1,6,7,4,5), group = c("a","a","b","b","upper_a","upper_a","lower_a","lower_a", 
"upper_b","upper_b","lower_b","lower_b"))
ggplot(data=dat , aes(x=as.factor(x), y=y,fill=group, group= group,
                                                      color = group )) + 
  geom_line()  + geom_point()+
  scale_fill_manual( name = "Metric",   labels = c(
                                  a = "a",
                                  b = "b",
                                  upper_a = "upper a",
                                  lower_a = "lower a",
                                  upper_b ="upper b",
                                  lower_b= "lower b"),
                                    values =c(
                                  a = "red",
                                  b = "blue",
                                  upper_a = "lightpink",
                                  lower_a = "lightpink",
                                  upper_b ="lightsteelblue",
                                  lower_b= "lightsteelblue")
                         ) +
  scale_color_manual( name = "Metric",   labels = c(
                                  a = "a",
                                  b = "b",
                                  upper_a = "upper a",
                                  lower_a = "lower a",
                                  upper_b ="upper b",
                                  lower_b= "lower b"),
                                    values =c(
                                  a = "red",
                                  b = "blue",
                                  upper_a = "lightpink",
                                  lower_a = "lightpink",
                                  upper_b ="lightsteelblue",
                                  lower_b= "lightsteelblue")
                         )

【问题讨论】:

  • 虽然您的数据很长,但它并不是一个nice 长。您的 group 列同时编码了 a/b 分组和 upper/lower/regular 分组 - jdobres 的回答很好地将这些分组分成不同的列,以便可以适当地使用它们。

标签: r ggplot2


【解决方案1】:

通常,ggplot 希望数据采用“长”格式。但是,要绘制诸如错误项之类的内容,您通常需要改变此规则,并为您的主要值和错误值设置单独的“宽”列。因此,首先,我们可以转换您现有的数据:

library(dplyr)
library(tidyr)

dat2 <- separate(dat, group, c('measure', 'group'), fill = 'right') %>% 
    mutate(group = ifelse(is.na(group), measure, group), measure = ifelse(measure %in% c('a', 'b'), 'value', measure)) %>% 
    spread(measure, y)

  x group lower upper value
1 1     a     0     2     1
2 1     b     4     6     5
3 2     a     1     3     2
4 2     b     5     7     6

然后使用这些新列绘制数据:

plot.new <- ggplot(data = dat2, aes(x = x, y = value, ymin = lower, ymax = upper, color = group)) +
    geom_point() +
    geom_line() +
    geom_errorbar(width = 1/5)
print(plot.new)

顺便说一句,如果错误是对称的,您可以通过使用aes() 中的数学表达式来重复使用单个错误列:

plot.new <- ggplot(data = dat2, aes(x = x, y = value, ymin = value - error, ymax = value + error, color = group)) + ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2011-04-10
    • 1970-01-01
    • 2012-05-06
    • 2013-11-27
    • 2022-01-25
    相关资源
    最近更新 更多