Home > Back-end >  How do I plot a graph by based on 3 groups in ggplot?
How do I plot a graph by based on 3 groups in ggplot?

Time:10-05

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!

CodePudding user response:

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

enter image description here

CodePudding user response:

For an interesting approach (though I would recommend you use whichever answer you find most straightforward), using interaction would allow grouping by those three variables without modifying the data sent to the ggplot function:

ggplot(df, aes(x = Laps, y = Timings, col = interaction(year, city, rank, sep = " ")))  
  geom_point()  
  geom_line()  
  scale_colour_discrete("Cohort")

This is little more than a "purist's" approach to ggplot - aiming to let the plotting functions do most of the work on a tidy dataset! Included here for interest.

Slight edit - you don't need to reformat labels if you use sep = " " in interaction call

  • Related