简体   繁体   中英

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!

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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