r - 如何在条形图中的误差条和条形之间进行颜色叠加?
问题描述
我有一个带有误差线的条形图,我想对条形图和误差线使用相同的颜色。但是,这意味着我看不到错误栏的底部。
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
?
解决方案
一个简单的阿尔法就可以了。
请注意,从技术上讲,您有不同的颜色。另一种选择是使用颜色修改包,例如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, 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)
推荐阅读
- python - 如何检查包含 6 个项目的列表中是否有 3 个或 4 个或 5 个连续数字?
- amazon-web-services - 如何修复提供程序 provider.aws 不支持数据源
- django-models - django latest() 按模板中的列表
- javascript - 如果我没有时间戳,AJAX 长轮询的最佳实践
- notepad++ - 在记事本++中使用正则表达式时卡住了
- python - Django 错误:/trade/confirm/ 处的 TemplateDoesNotExist
- python - 从现有列创建新列
- python - 如何编写函数计算python中未绑定数据集的运行平均值?
- aframe - 如何在帧中使用运动记录?
- javascript - 预期 2 个参数,但得到 1.ts(2554) index.ts(54, 112):未提供 'arg1' 的参数