Home > Software design >  How to visualize average hourly data for each day in a week as a facet_wrap of seven days in R?
How to visualize average hourly data for each day in a week as a facet_wrap of seven days in R?

Time:11-03

I have a long data frame consisting of the following columns: Date, hour, day, week, weekdays, Values

Here it is:

df <- structure(list(Date = structure(c(1482087600, 1482084000, 1482080400, 
1482076800, 1482073200, 1482069600, 1482066000, 1482062400, 1482058800, 
1482055200), class = c("POSIXct", "POSIXt"), tzone = ""), hour = 23:14, 
    day = c(18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L), 
    week = c(51, 51, 51, 51, 51, 51, 51, 51, 51, 51), weekdays = c("Sunday", 
    "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", 
    "Monday", "Monday", "Monday"), Value= c(18L, 20L, 25L, 
    29L, 31L, 32L, 30L, 23L, 24L, 17L)), row.names = c(NA, 10L
), class = "data.frame")
df

I want to visualize hours in a facet_wrap for seven weekdays by averaging the data in the "Value" column for each hour of each day. For example, hour 2 on Monday should include hourly average for 2 for each Monday, hour 3 on Tuesday should include hourly average for hour 3 for each Tuesday and so on.

So far, my code looks like this:

df%>%
  group_by(day) %>%
  group_by(hour)
  summarise(avg_hour = mean(Value)) %>%
  ggplot(aes(x=hour, y=Value, color = weekdays))   
  geom_line()   
  ylab("Value")   
  xlab("Hours")

However, I keep receiving this Error:

Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): x = hour. 
Did you mistype the name of a data column or forget to add after_stat()?

I would greatly appreciate it if someone could help me get the correct plot.

CodePudding user response:

You can get the plot you're after using:

df %>%
  group_by(day) %>%
  group_by(hour) %>%
  mutate(avg_hour = mean(Value)) %>%
  ungroup() %>%
  ggplot(aes(x=hour, y=avg_hour))   
  geom_line()   
  ylab("Value")   
  xlab("Hours")  
  facet_wrap(vars(weekdays))

enter image description here


You had a few issues with your code:

  • Your group_by was missing a pipe into the summarise
  • You want to plot your derived column avg_hour, not the original column Value
  • summarise() deletes all columns that aren't either grouping columns or produced by summarise, so weekdays wasn't available. Hence I used mutate() %>% ungroup() instead
  • You missed the actual facet_wrap(), which I added.
  • Related