r - 特殊堆积条形图 R ggplot
问题描述
你能帮我在 R 中制作以下条形图吗?我有一些简化的虚拟数据用于重新创建,然后我的计划是以相同的方式操作数据。不需要做abline。最重要的部分是瀑布方面。
ï..labels value
1 start 100
2 january 120
3 febuary 140
4 march 160
5 april 180
6 may 130
7 june 140
8 july 170
9 august 160
10 september 180
11 october 190
12 november 210
13 december 200
14 end 200
解决方案
这会让你得到瀑布效应:
library(tidyverse)
df <-
tibble::tribble(
~month, ~month_name, ~value,
1, "start", 100,
2, "january", 120,
3, "febuary", 140,
4, "march", 160,
5, "april", 180,
6, "may", 130,
7, "june", 140,
8, "july", 170,
9, "august", 160,
10, "september", 180,
11, "october", 190,
12, "november", 210,
13, "december", 200,
14, "end", 200
) %>%
mutate(
type = case_when(
month == min(month) ~ "Initial",
month == max(month) ~ "Final",
value > lag(value) ~ "Increase",
TRUE ~ "Decrease"
),
finish = value,
start = if_else(month == max(month), 0, replace_na(lag(value), 0))
)
df %>%
ggplot(aes(xmin = month - 0.3, xmax = month + 0.3, ymin = start, ymax = finish, fill = type)) +
geom_rect() +
scale_x_continuous(
breaks = 1:14,
labels = df %>% select(month_name) %>% pull()
) +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none"
)
您应该能够从这里处理格式和颜色;)
推荐阅读
- go - URL 的非转义查询参数
- r - 如何根据其他属性降低因子水平?
- docker - 运行一个 Docker 容器而不是其他容器。码头工人组成
- ansible - 如何在不同的主机上运行不同的任务?
- java - 如何从 Web 服务器检索图像(图像/png 标头)
- azure-service-fabric - Service Fabric:无法配置证书权限 - FABRIC_E_CERTIFICATE_NOT_FOUND
- python - 如何在pyopengl中使用鼠标旋转立方体
- python - 将昵称与 Pandas 中的名称匹配
- git - 特定数量的提取后,带有 Jenkins 的 Git 插件在“git reset --hard”中失败
- firebase - 在不使用 .getDownloadUrl 的情况下访问 Firebase 存储映像