美學必須是長度 1 或與資料 (5) 相同:填充、y 和 軸 1 任何建議都會被采納。謝謝!
這是背景:
dim(fly)
Rows: 21,000
Columns: 4
head(fly,5)
Date. Airport. Count Type
2022-01-02 Brussels 256 Arrival
2022-01-24 Charleroi 84 Departure
2022-02-03 Berlin 148 Departure
2022-03-18 Dresden 95 Arrival
2022-03-19 Erfurt 29 Departure
2022-04-01 Frankfurt 391 Departure
structure(list(Date = structure(c(1640995200, 1640995200,
1640995200, 1640995200, 1640995200, 1640995200, 1640995200, 1640995200,
1640995200, 1640995200), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Airport = c("Brussels", "Charleroi", "Berlin - Brandenburg",
"Dresden", "Erfurt", "Frankfurt", "Muenster-Osnabrueck", "Hamburg",
"Cologne-Bonn", "Dusseldorf"), Count = c(148, 54, 148,
5, 0, 391, 6, 78, 60, 103), Type = c("Departure", "Departure",
"Arrival", "Departure", "Arrival", "Arrival", "Departure",
"Arrival", "Departure", "Arrival")), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
要求是僅使用沖積層比較來自 3 個機場(布魯塞爾、倫敦、德累斯頓)的到達航班數量。
下面的代碼有效,但它產生的是總(5個月)計數,而不是每個月/機場的總數。
df_fly <- filter(fly, Airport %in% c("Brussels", "Dresden", "London"), Type =="Arrival") %>%
group_by(Airport) %>%
summarise(Flight_Count = sum(Flight_Count))
df_fly<- as.data.frame(df_fly)
ggplot(df_fly,
aes(y = Count, axis1 = Airport, axis2 =Count)) geom_alluvium(aes(fill = Airport), width = 1/8)
geom_stratum(width = 1/8, fill = "black", color = "grey") geom_label(stat = "stratum", aes(label = after_stat(stratum))) scale_x_discrete(limits = c("Airport", "Count"),
expand = c(.05, .05))
scale_fill_brewer(type = "qual", palette = "Set2")
ggtitle("Arrival Flight Comparison")
然后我嘗試使用這個來填充每個機場每月的方式,但它產生了一個錯誤:
美學必須是長度 1 或與資料 (5) 相同:填充、y 和軸 1
df_fly <- filter(fly, Airport %in% c("Brussels", "Dresden", "London"), Type =="Arrival") %>%
group_by(month = lubridate::floor_date(Date, 'month')) %>%
summarize(Count = sum(Count))
df_fly<- as.data.frame(df_fly)
ggplot(df_fly,
aes(y = Count, axis1 = Airport, axis2 =Count)) geom_alluvium(aes(fill = Airport), width = 1/8)
geom_stratum(width = 1/8, fill = "black", color = "grey") geom_label(stat = "stratum", aes(label = after_stat(stratum))) scale_x_discrete(limits = c("Airport", "Count"),
expand = c(.05, .05))
scale_fill_brewer(type = "qual", palette = "Set2")
ggtitle("Arrival Flight Comparison")
uj5u.com熱心網友回復:
所以如果沒有reprex,它會有點難以除錯。我的第一個建議是嘗試格式化您的代碼,使其更易于閱讀,垂直空間是免費的!我試著在下面重寫它。
我認為主要問題是group_by() call
,你想按機場和月份的每個組合進行分組,所以應該是group_by(Airport, month)
。如果不按機場分組,匯總后您會丟失機場列,因此 ggplot 中的錯誤,因為它找不到機場列。讓我知道以下代碼是否有效:
df_fly = fly %>%
filter(
Airport %in% c("Brussels", "Dresden", "London"),
Type == "Arrival"
) %>%
group_by(
Airport,
month = lubridate::floor_date(Date, 'month'),
) %>%
summarize(Count = sum(Count))
df_fly %>%
ggplot(aes(y = Count, axis1 = Airport, axis2 = Count))
geom_alluvium(
aes(fill = Airport),
width = 1/8
)
geom_stratum(
width = 1/8,
fill = "black",
color = "grey"
)
geom_label(
stat = "stratum",
aes(label = after_stat(stratum))
)
scale_x_discrete(
limits = c("Airport", "Count"),
expand = c(.05, .05))
scale_fill_brewer(
type = "qual",
palette = "Set2"
)
ggtitle("Arrival Flight Comparison")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522285.html
標籤:rggplot2冲积层