realbeast91
realbeast91

Reputation: 176

Multiple line in graph with year as color

I have the following dataset

structure(list(dteday = c("00:00", "00:00", "01:00", "01:00", 
"02:00", "02:00", "03:00", "03:00", "04:00", "04:00", "05:00", 
"05:00", "06:00", "06:00", "07:00", "07:00", "08:00", "08:00", 
"09:00", "09:00", "10:00", "10:00", "11:00", "11:00", "12:00", 
"12:00", "13:00", "13:00", "14:00", "14:00", "15:00", "15:00", 
"16:00", "16:00", "17:00", "17:00", "18:00", "18:00", "19:00", 
"19:00", "20:00", "20:00", "21:00", "21:00", "22:00", "22:00", 
"23:00", "23:00"), yr = c("2011", "2012", "2011", "2012", "2011", 
"2012", "2011", "2012", "2011", "2012", "2011", "2012", "2011", 
"2012", "2011", "2012", "2011", "2012", "2011", "2012", "2011", 
"2012", "2011", "2012", "2011", "2012", "2011", "2012", "2011", 
"2012", "2011", "2012", "2011", "2012", "2011", "2012", "2011", 
"2012", "2011", "2012", "2011", "2012", "2011", "2012", "2011", 
"2012", "2011", "2012"), cnt = c(15537L, 23590L, 9556L, 14606L, 
6660L, 9691L, 3441L, 4733L, 1817L, 2611L, 5154L, 9104L, 20789L, 
34331L, 56893L, 97241L, 95423L, 165534L, 59462L, 99952L, 47494L, 
78746L, 56587L, 94722L, 69164L, 115216L, 69327L, 115580L, 66470L, 
109170L, 68576L, 114559L, 85601L, 142126L, 127521L, 209228L, 
116903L, 192787L, 85746L, 140987L, 62780L, 101732L, 48847L, 76570L, 
37643L, 57935L, 25089L, 38825L)), row.names = c(NA, -48L), groups = structure(list(
    dteday = c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", 
    "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", 
    "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", 
    "20:00", "21:00", "22:00", "23:00"), .rows = structure(list(
        1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 
        19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34, 
        35:36, 37:38, 39:40, 41:42, 43:44, 45:46, 47:48), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 24L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

And this is the graph I currently have

ggplot(data=bike3,
       aes(x=dteday, y=cnt, colour=yr, group = 2)) +
       geom_line() + 
       labs(title = "Bike Rentals Per Hour",
       x = "Hour",
       y = "Count")+ 
  theme_solarized() +
  theme(axis.text.x=element_text(angle=45, hjust=1))

enter image description here

I want there to be 2 distinct lines; 2011 (red) and 2012 (blue). My current graph has both lines mixed together and I am not sure how to fix it

Upvotes: 1

Views: 47

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389135

If you want plot to be continuous i.e one year after another you can combine yr and dteday.

library(dplyr)
library(ggplot2)

bike3 %>%
  ungroup() %>%
  mutate(dtime = paste(yr, dteday)) %>%
  arrange(dtime) -> bike4


ggplot(bike4, aes(x=dtime, y=cnt, colour=yr, group = 1)) +
  geom_line() + 
  labs(title = "Bike Rentals Per Hour",
       x = "Hour",
       y = "Count")+ 
  theme(axis.text.x=element_text(angle=45, hjust=1)) + 
  scale_x_discrete(breaks = bike4$dtime[c(TRUE, FALSE)])

enter image description here

Upvotes: 2

jjmerelo
jjmerelo

Reputation: 23537

Just group by year yr:

ggplot(data=bike3,
       aes(x=dteday, y=cnt, colour=yr, group = yr)) +
       geom_line() + 
       labs(title = "Bike Rentals Per Hour",
       x = "Hour",
       y = "Count")+ 
  theme_solarized() +
  theme(axis.text.x=element_text(angle=45, hjust=1))

Plotting bike rentals per hour

Upvotes: 2

Related Questions