简体   繁体   English

如何构建堆积条形图

[英]How to build stacked bar chart

How can I build stacked bar chart from this data?如何从这些数据中构建堆积条形图? Where years will be x axis while Old and NEW differentiated via colours in bars.其中年份将是 x 轴,而旧和新通过条形中的颜色进行区分。 However I want to avoid manual coding and automatize the process但是我想避免手动编码并使过程自动化

structure(list(`1998` = c(11, 826), `2000` = c(217, 620), `2007` = c(625, 
212), `2012` = c(836, 1)), class = "data.frame", row.names = c("NEW", 
"OLD"))


    1998 2000 2007 2012
NEW   11  217  625  836
OLD  826  620  212    1

Expected output:预期 output:

[![在此处输入图像描述][1]][1] [1]:https://i.stack.imgur.com/l2Y59.png

Looking for something like this?寻找这样的东西?

library(tidyverse)
df %>%
  # rownames to column
  mutate(type = rownames(.)) %>%
  # convert to long data
  pivot_longer(-"type") %>%
  # plot
  ggplot() + 
  geom_col(aes(x = name, y = value, fill = type))

在此处输入图像描述

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

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