gfg_data <- data.frame(year = c(2019, 2019, 2019, 2020, 2020, 2020, 2019, 2019, 2019, 2020, 2020, 2020),
city = c('Paris', 'Paris', 'Paris', 'Paris', 'Paris', 'Paris', 'Baku', 'Baku', 'Baku', 'Baku', 'Baku', 'Baku'),
rank = c('1-3', '1-3', '1-3', '4-6', '4-6', '4-6', '1-3', '1-3', '1-3', '4-6', '4-6', '4-6'),
Timings = c(5, 6, 4, 2, 3, 4, 11, 13, 15, 14, 17, 12),
Laps = c(1,2,3,1,2,3,1,2,3,1,2,3))
I would like to plot this data with laps on the x-axis and Timings on the y-axis. I'd like to use geom_line()
and group the graph by city, rank, and year. So, I should have a line for 2019/Paris/1-3, 2019/Paris/4-6, 2020/Paris/1-3, 2020/Paris/4-6, etc. I'm unsure how I'd be able to do this using ggplot
. Any help would be appreciated!
From your description, it sounds like you need to combine year, city, and rank into a new variable.
library(tidyverse)
df <- data.frame(year = c(2019, 2019, 2019, 2020, 2020, 2020, 2019, 2019, 2019, 2020, 2020, 2020),
city = c('Paris', 'Paris', 'Paris', 'Paris', 'Paris', 'Paris', 'Baku', 'Baku', 'Baku', 'Baku', 'Baku', 'Baku'),
rank = c('1-3', '1-3', '1-3', '4-6', '4-6', '4-6', '1-3', '1-3', '1-3', '4-6', '4-6', '4-6'),
Timings = c(5, 6, 4, 2, 3, 4, 11, 13, 15, 14, 17, 12),
Laps = c(1,2,3,1,2,3,1,2,3,1,2,3)) %>%
mutate(value = paste(year, city, rank))
ggplot(df, aes(x = Laps, y = Timings, col = value)) +
geom_point() +
geom_line()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments