特殊堆积条形图R ggplot

您能帮我在R中制作以下条形图吗?我有一些简化的伪数据用于重新创建,然后我的计划是以相同的方式处理数据。无需进行退位。最重要的部分是瀑布方面。

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

特殊堆积条形图R ggplot

csl6868926 回答:特殊堆积条形图R ggplot

这为您带来瀑布效果:

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",8,"july",170,9,"august",10,"september",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),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"
  )

您应该可以从这里开始照顾格式和颜色;)

enter image description here

本文链接:https://www.f2er.com/3092094.html

大家都在问