当缺少值时按组躲避geom_errorbar

问题描述 投票:1回答:2

我正在尝试绘制不同的点,其中一些是观察值(因此没有误差线),另一些是预测(有误差线)。我使用position_dodge放置点,但是由于误差线缺少值,因此无法找到将误差线与它们各自的点匹配的方法。

下面,我尝试根据我的数据集制作一个简单的可复制示例。

a <- data.frame(taxon = "plants", type = c(rep("observation", 3), "prediction"), period = c("1970-2017", "2000-2009", "2010-2017", "2017"), value = 1:4, lwr = c(NA, NA, NA, 3.5), upr = c(NA, NA, NA, 4.5))

a
#>    taxon        type    period value lwr upr
#> 1 plants observation 1970-2017     1  NA  NA
#> 2 plants observation 2000-2009     2  NA  NA
#> 3 plants observation 2010-2017     3  NA  NA
#> 4 plants  prediction      2017     4 3.5 4.5

这是我用于ggplot的代码:

ggplot(a) +
  geom_point(aes(x = taxon, 
                 shape = type,
                 y = value,
                 col = period),
             position = position_dodge(width = .5)) +
  geom_errorbar(aes(x = taxon, 
                    ymin = lwr, ymax = upr),
                position = position_dodge(width = .5))

ggplot output

您可以看到,错误栏居中,最可能是因为lwrupr中缺少的值已省略,但应在右上角。到目前为止,我为解决此问题所做的所有尝试(即使用position_dodge进行不同的设置,尝试指定preserve参数)均未成功,并且我无法在Internet上找到帮助。

r ggplot2
2个回答
2
投票

这可能不是最优雅的解决方案,但是您可以改用geom_pointrange并使upr和lwr值与您的value列相同,以便在绘制时不会出现误差线。

例如

    a$lwr <- ifelse(is.na(a$lwr), a$value, a$lwr)
    a$upr <- ifelse(is.na(a$upr), a$value, a$upr)

    ggplot(a) +
      geom_pointrange(aes(x=taxon, y=value, ymin=lwr, ymax=upr, shape=type, col=period), 
                  position = position_dodge(width = 0.5)) +
      theme_bw()

这给出了这张图,听起来像您想要的:“指针范围示例”


2
投票

我可能不依赖于按组躲避,只需将x变量更改为interaction(taxon,period)。然后,您可以删除闪避,它将看起来像这样:

a <- data.frame(taxon = "plants", type = c(rep("observation", 3), "prediction"), period = c("1970-2017", "2000-2009", "2010-2017", "2017"), value = 1:4, lwr = c(NA, NA, NA, 3.5), upr = c(NA, NA, NA, 4.5))

library(ggplot2)
ggplot(a) +
  geom_point(aes(x = interaction(taxon, period), shape = type, y = value, col = period)) + 
  geom_errorbar(aes(x = interaction(taxon, period), ymin = lwr, ymax = upr))
#> Warning: Removed 3 rows containing missing values (geom_errorbar).

“”

reprex package(v0.3.0)在2020-01-30创建

根据评论编辑

如果您有一个以上的分类单元,那么从各个方面来看,一种非常整洁的方法就太过分离了。

a <- data.frame(taxon = "plants", type = c(rep("observation", 3), "prediction"), period = c("1970-2017", "2000-2009", "2010-2017", "2017"), value = 1:4, lwr = c(NA, NA, NA, 3.5), upr = c(NA, NA, NA, 4.5))
b <- data.frame(taxon = "plants_b", type = c(rep("observation", 3), "prediction"), period = c("1970-2017", "2000-2009", "2010-2017", "2017"), value = 1:4, lwr = c(NA, NA, NA, 3.5), upr = c(NA, NA, NA, 4.5))

library(ggplot2)
ggplot(rbind(a,b)) +
  geom_point(aes(x = interaction(taxon, period), shape = type, y = value, col = period)) + 
  geom_errorbar(aes(x = interaction(taxon, period), ymin = lwr, ymax = upr)) +
  facet_grid(~taxon, scales = 'free_x') +
  theme(axis.text.x = element_blank())
#> Warning: Removed 6 rows containing missing values (geom_errorbar).

“”

reprex package(v0.3.0)在2020-01-30创建

我提供了免费的x刻度并删除了x标签,因为它不包含刻面标题或颜色图例中尚未包含的其他信息

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.