Home > Back-end >  using geom_line() to show two lines from one data frame
using geom_line() to show two lines from one data frame

Time:03-26

I have a data frame. df

inside the data frame there is a column called cars inside of cars there are two attributes(?) red and blue. so it looks something like this

day_of_week cars
monday      red
tuesday     blue
monday      red
sunday      red 
saturday    blue
friday      red

I am trying to plot days_of_week using ggplot geom_line() using cars code to show the count of red and blue for each day of the week.

ggplot(df, aes(day_of_week, color = cars)) geom_line(stat = "count") but i get the error geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? and the graph just comes up blank. I managed to fix it by splitting up red and blue into to df because if i use group =1 it just combines them together into one line. if i use group = cars there is no overlap with the lines

df_red <- filter(all_trip_v3, cars == "red")
df_blue <- filter(all_trip_v3, cars == "blue")

ggplot(df_red, aes(day_of_week, group =1, color = cars))   geom_line(stat = "count")  
 geom_line(data = df_blue, mapping = aes(day_of_week, group =1, color = cars), stat = "count")

this fixed it. but is there a better way to do it from one data frame? instead of having to split it into two?

CodePudding user response:

I seem to have found an answer to my own question.

After doing more searching I was able to combine and show both lines the way I wanted to using group = interaction(cars)

so my code looks like : ggplot(df, aes(day_of_week, group = interaction(cars), color = cars)) geom_line(stat = "count")

  • Related