Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/l2Y59.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading