我有一個由以下列組成的長資料框:日期、小時、日、周、作業日、值
這里是:
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
我想通過平均每天每個小時的“值”列中的資料來可視化 7 個作業日的 facet_wrap 中的小時數。例如,星期一的第 2 小時應包括每個星期一 2 的每小時平均數,星期二的第 3 小時應包括每個星期二的第 3 小時的每小時平均數,依此類推。
到目前為止,我的代碼如下所示:
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")
但是,我不斷收到此錯誤:
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()?
如果有人能幫我得到正確的情節,我將不勝感激。
uj5u.com熱心網友回復:
你可以得到你使用后的情節:
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))

您的代碼存在一些問題:
- 你
group_by錯過了一個管道summarise - 您想繪制您的派生列
avg_hour,而不是原始列Value summarise()洗掉所有既不是分組列也不是由匯總生成的列,因此weekdays不可用。因此我mutate() %>% ungroup()改用- 你錯過了
facet_wrap()我添加的實際。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/346335.html
上一篇:如果前面沒有相同字符,則替換字符
