I have a bar chart with errorbars, and I want to use the same color for both bar and errorbar. However, this would mean that I could not see the bottom part of the errorbar.
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")
Is there a way to achieve such overlay using ggplot
?
A simple alpha would do.
Note you technically have different colors. Another option would be to use color modifying packages such as shades
or colorspaces
. See one option with colorspaces
below. shades
is cool when you want to change entire palettes.
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)