ggplot2 中的颜色误差线

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

对于示例数据框:

df <- structure(list(site = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 
                                         3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 2L, 2L, 2L, 2L), .Label = c("1 - JL", 
                                                                                                         "11 - KT", "2 - SSD", "3 - USSD", "4 - MES"), class = "factor"), 
                      x = c(3L, 4L, 2L, 2L, 4L, 6L, 1L, 3L, 6L, 4L, 4L, 5L, 3L, 
                            3L, 3L, 1L, 2L, 5L, 4L, 1L), y = c(3L, 5L, 3L, 5L, 4L, 3L, 
                                                               5L, 2L, 6L, 4L, 3L, 5L, 5L, 5L, 6L, 3L, 4L, 3L, 4L, 2L), 
                      type = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 3L, 4L, 3L, 
                                         3L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("a", 
                                                                                             "b", "c", "d"), class = "factor"), y_upper.error = c(2L, 
                                                                                                                                                  4L, 2L, 4L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L, 4L, 4L, 5L, 2L, 
                                                                                                                                                  3L, 2L, 3L, 1L), y_lower.error = c(4.5, 6.5, 4.5, 6.5, 5.5, 
                                                                                                                                                                                     4.5, 6.5, 3.5, 7.5, 5.5, 4.5, 6.5, 6.5, 6.5, 7.5, 4.5, 5.5, 
                                                                                                                                                                                     4.5, 5.5, 3.5)), .Names = c("site", "x", "y", "type", "y_upper.error", 
                                                                                                                                                                                                                 "y_lower.error"), class = "data.frame", row.names = c(NA, -20L
                                                                                                                                                                                                                 ))

我正在创建一个带有误差线的图表。

g <- ggplot (df, aes(x=x, y=y, shape = type)) +
  geom_point() +
  scale_shape_manual(values=c(21,15,25,18)) + #makes open circle/triangle
  facet_wrap(~site, ncol=2)+
  geom_errorbar(aes(ymin=y_lower.error, ymax=y_upper.error), colour="red")

g

我希望为误差线的不同部分着色 - 即。下面蓝色上面红色? 是否可以通过这种方式为误差条的每个单独尾部着色?

r ggplot2
2个回答
2
投票

这是一个黑客解决方案,将误差线分成两部分(下尾到 y,y 到上尾)并分别为它们着色:

ggplot(df2, aes(x=x, y=y, shape = type))+
  geom_errorbar(aes(ymin=y, ymax=y_upper.error), colour="blue", width = 0.1) +
  geom_errorbar(aes(ymin=y_lower.error, ymax=y), colour="red", width = 0.1) +
  geom_point() +
  scale_shape_manual(values=c(21,15,25,18)) + #makes open circle/triangle
  facet_wrap(~site, ncol=2)

dual-color error bar plot

我缩短了误差条尾部的宽度,以便它们(大约)被

geom_point
覆盖。


0
投票

superb
(版本0.95.19)有一个geom
geom_superberrorbar()
,可以使用参数
vcolour
wcolour
对两半进行不同的着色。您还可以有多个提示并决定它们的方向:

library(superb)
library(ggplot2)

dta <- data.frame(grp = c(1,2,3), center=c(1,2,3), width = c(1,1,1.5) )

ggplot(dta, aes(ymin=center-width, ymax=center+width, x = grp ) ) +
  geom_superberrorbar(tipformat = "triple", width= 0.1, 
                      tipgap = 0.04, direction = "left",
                      colour = "black", 
                      vcolour = "purple", wcolour="red")

error bars with three tips left point and two colors

© www.soinside.com 2019 - 2024. All rights reserved.