如何在条形图中的 errorbar 和 bar 之间进行颜色叠加?

How to do color overlay between errorbar and bar in a bar chart?

我有一个带误差条的条形图,我想对条形图和误差条使用相同的颜色。但是,这意味着我看不到错误栏的底部。

library(ggplot2)
library(tibble)

my_df <- 
  tibble::tribble(~response, ~estimate, ~lower_ci, ~upper_ci,
                  "little_bit", 0.353477, 0.255625, 0.451747,
                  "no", 0.307639, 0.250436, 0.375393,
                  "very", 0.338883, 0.301007, 0.37572310)

## compare this:
ggplot(my_df, aes(x = reorder(response, -estimate), y = estimate)) +
  geom_bar(stat = "identity", width = 0.9, fill = "#6EB3FF") +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0.1, size = 3.5)

## with this:
ggplot(my_df, aes(x = reorder(response, -estimate), y = estimate)) +
  geom_bar(stat = "identity", width = 0.9, fill = "#6EB3FF") +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0.1, size = 3.5, color = "#6EB3FF")


我的视觉想法是使用覆盖



有没有办法使用 ggplot 实现这种叠加?

一个简单的 alpha 就可以了。

注意你在技术上 不同的颜色。另一种选择是使用颜色修改包,例如 shadescolorspaces。请参阅下面带有 colorspaces 的选项。 shades 当您想更改整个调色板时很酷。

library(ggplot2)
library(tibble)

my_df <- 
  tibble::tribble(~response, ~estimate, ~lower_ci, ~upper_ci,
                  "little_bit", 0.353477, 0.255625, 0.451747,
                  "no", 0.307639, 0.250436, 0.375393,
                  "very", 0.338883, 0.301007, 0.37572310)

# super easy, just make the bars mor transparent - not quite your desired look
ggplot(my_df, aes(x = reorder(response, -estimate), y = estimate)) +
  geom_bar(stat = "identity", width = 0.9, fill = "#6EB3FF", alpha  = 0.7) +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0.1, size = 3.5,color = "#6EB3FF")


## darkening the color, and adding some alpha for your desired effect
ggplot(my_df, aes(x = reorder(response, -estimate), y = estimate)) +
  geom_bar(stat = "identity", width = 0.9, fill = "#6EB3FF") +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0.1, size = 3.5, 
                color = colorspace::darken("#6EB3FF"), alpha  = 0.7)

也许,您应该用稍微深一点的颜色再次绘制误差线的下半部分。

ggplot(my_df, aes(x = reorder(response, -estimate), y = estimate)) +
  geom_bar(stat = "identity", width = 0.9, fill = "#6EB3FF") +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0.1, size = 3.5, color = "#6EB3FF") + 
  geom_errorbar(aes(ymin = lower_ci, ymax = estimate-0.004),
                width = 0.1, size = 3.5, color="#3ba8ff")