Home > front end >  r - ggplot multiple line graphs using all column as x and all row as y
r - ggplot multiple line graphs using all column as x and all row as y

Time:01-18

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!

CodePudding user response:

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

  •  Tags:  
  • Related