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

r – ggplot multiple line graphs using all column as x and all row as y

I have a tbl_df that looks like

| Type| 2015 | 2016 | 2017 |2018 |
| One  | 10000| 165274| 268709| 332536|
| Two  | 6763 | 46996 | 59183 | 34896 |
| Three| 8316 | 23347 | 45878 | 49054 |

How can I use ggplot to make a multiple line graph, with each column that has year as the x-axis, and each Type as a new line, as the y axis? Thanks!

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 :

For ggplot it is convenient to plot if you bring the data in long format.

library(tidyverse)

df %>%
  pivot_longer(cols = -Type) %>%
  ggplot(aes(name, value, color = Type, group = Type)) + 
  geom_line() + 
  labs(x = 'Year', y = 'Value', 
       title = "Values for different types")

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