问题描述
我有一个带有误差条的条形图,我想为条形图和误差条使用相同的颜色。但是,这意味着我看不到误差条的底部。
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,size = 3.5,color = "#6EB3FF")
我的视觉想法是使用叠加
有没有办法使用 ggplot
实现这种叠加?
解决方法
一个简单的 alpha 就可以了。
请注意,技术上有不同的颜色。另一种选择是使用颜色修改包,例如 shades
或 colorspaces
。请参阅下面带有 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,fill = "#6EB3FF") +
geom_errorbar(aes(ymin = lower_ci,color = colorspace::darken("#6EB3FF"),alpha = 0.7)
,
也许,您应该再次用稍微深一点的颜色绘制误差线的下半部分。
ggplot(my_df,color = "#6EB3FF") +
geom_errorbar(aes(ymin = lower_ci,ymax = estimate-0.004),color="#3ba8ff")