我正在嘗試檢查一年中每個月每天從紐約市起飛的航班分布。我使用的資料集是“nycflights13”,它可以作為一個包安裝。然后,我將資料轉換如下:
# load
flights <- nycflights13::flights
# rid NA
flights <- flights %>% tidyr::drop_na(tailnum)
# filter out only flights going FROM NYC and add month
flights_with_month <- flights %>%
filter(origin != "EWR") %>%
mutate(mth = month(time_hour), label = TRUE)
# calculate flights per day
flights_with_month <- flights_with_month %>%
group_by(mth, day) %>%
mutate(total_daily_flights = n())
# making boxplots
ggplot(flights_with_month, aes(y = total_daily_flights, group = mth))
geom_boxplot()
這是我得到的箱線圖;您會注意到 x 軸不是月份的名稱,而且它們似乎也不是從 1 月到 12 月的順序。
uj5u.com熱心網友回復:
您的代碼有兩個問題。mutate(mth = month(time_hour), label = TRUE)
應該是mutate(mth = month(time_hour, label = TRUE))
,并且您的 ggplot 應該設定x = mth
而不是grp = mth
. 資料以正確的順序繪制,但標簽不正確。
# load
flights <- nycflights13::flights
# rid NA
flights <- flights %>% tidyr::drop_na(tailnum)
# filter out only flights going FROM NYC and add month
flights_with_month <- flights %>%
filter(origin != "EWR") %>%
mutate(mth = month(time_hour, label = TRUE))
# calculate flights per day
flights_with_month <- flights_with_month %>%
group_by(mth, day) %>%
mutate(total_daily_flights = n())
# making boxplots
ggplot(flights_with_month, aes(y = total_daily_flights, x = mth))
geom_boxplot()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462743.html
上一篇:如何在ggplot中為變數分組?
下一篇:繪制時間序列資料