How can I solve this problem in Geom_line?

38 views Asked by At
day_of_week feb_23  mar_23  apr_23  may_23  jun_23      
<chr>           <int>   <int>   <int>   <int>   <int>           
sunday      24575   48314   83366   84074   116821      
monday      32208   46043   78434   85285   122528      
tuesday     41347   56317   101060  76440   101694      
wednesday   25792   51056   61445   98910   94837       
thursday    21852   46027   71823   83957   117189      
friday      38517   69504   75012   131155  104023      
saturday    24284   24948   73140   84078   130631      

I am trying to do a line graph with theses informations, but I can't do it. I would like to use each day of week in a line to represent each month.

I've tried several ways but I deleted the previous ones. The last one was:

ggplot(trip_id, aes(y=day_of_week))+
       geom_line(aes(x=feb_23))+
       geom_line(aes(x=mar_23))+
       geom_line(aes(x=apr_23))
1

There are 1 answers

0
Seth On

First pivot the data to a long format, then you can plot the values on the y-axis for each month, defining the groups by the day_of_week.

library(tidyverse)

df %>%
  pivot_longer(-day_of_week,
               names_to = 'month') %>%
  ggplot(aes(x = month, y = value, color = day_of_week, group = day_of_week)) +
  geom_point() +
  geom_line()

Data

df <- read.table(header = TRUE, text = 
                   'day_of_week feb_23 mar_23 apr_23 may_23 jun_23

sunday 24575 48314 83366 84074 116821
monday 32208 46043 78434 85285 122528
tuesday 41347 56317 101060 76440 101694
wednesday 25792 51056 61445 98910 94837
thursday 21852 46027 71823 83957 117189
friday 38517 69504 75012 131155 104023
saturday 24284 24948 73140 84078 130631

'
                   )