How do I plot a graph by based on 3 groups in ggplot?

EV_Mustang
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!

jrcalabrese

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I plot US cities using ggplot?

How do I plot a bar graph using Pandas?

How do I facet a ggplot2 based graph from qicharts2 package?

How do I plot the the rows of a matrix as points on a graph?

How do I plot a Bar graph when comparing the rows?

How do I rotate a plot clockwise with ggplot?

In ggplot, how can I plot data as a line graph?

How do I reproduce a plot in ggplot based on nonlinear regressions from the R package Growthrates?

How do I plot pie chart graph with pandas data

How do I get a single percentage/proportion plot using ggplot for separate groups?

How do I plot an image from phylopic in top right corner of my ggplot graph in R?

How do i plot the graph side by side for comparison?

How do I render objects items in groups based on values

How do I plot a graph through an indexed time in matlab?

How can I do a multiple plot with a somSC type graph?

How can I plot the relative proportions of two groups using a fill aesthetic in ggplot2?

How do I create groups based on the sum of values?

how to plot graph based on attendance

How do I plot a line graph based off the 'Type' column? I want a line for A and line for B based off the 'Price' Column

How do I plot a graph of previously grouped data?

How do I plot multiple files in a single graph?

ggplot bar plot by multiple groups + line graph

How can I plot a grouped barplot in ggplot based on frequency?

How do I plot the graph for differentiation of a function on Octave?

Pandas/Matplotlib: How do I plot in groups and color code based on another column?

How to only select the top N groups based on total count to plot using ggplot in R

How do I make a plot with 3 scatterplots?

How do I plot the graph of `res ` for different `epsilon` in the same plot?

How can I add patterns to my ggplot2 graph that relate to my "fill" groups?