简体   繁体   English

特殊堆积条形图 R ggplot

[英]Special Stacked Bar Chart R ggplot

Can you help me make the following bar chart in R?您能帮我在 R 中制作以下条形图吗? I have some simplified dummy data that i am using to recreate, and then my plan is to manipulate the data in the same way.我有一些简化的虚拟数据用于重新创建,然后我的计划是以相同的方式操作数据。 No need to do the abline.不需要做abline。 The most important parts are the waterfall aspect.最重要的部分是瀑布方面。

   ï..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

在此处输入图像描述

This gets you the waterfall effect:这会让你得到瀑布效应:

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"
  )

You should be able to take care of the formatting and colors from here;)您应该能够从这里处理格式和 colors;)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM