我正在使用“nycflights13”软件包中的“航班”数据集。我被要求从原始代码转换为包含“stat_summary_2d”的代码:
flights %>%
mutate(cancel = 1*(dep_time %>% is.na)) %>%
group_by(carrier, origin) %>%
summarise(cancel = 100*mean(cancel, na.rm = T)) %>%
ggplot() +
geom_tile(aes(origin, carrier, fill = cancel)) +
geom_text(aes(origin, carrier, label = round(cancel,1)),
col = "blue", size = 5) +
scale_fill_distiller("Cancel Ratio", palette = "RdYlGn") +
theme_bw()在数据集中,“dep_time”变量的缺失值表示取消的航班,而变量“cancel”是通过计算取消航班的比例来创建的。下面是我如何应用“stat_summary_2d”来转换原始代码:
flights %>%
mutate(cancel = 1*(dep_time %>% is.na)) %>%
ggplot() +
stat_summary_2d(aes(carrier, origin, z = cancel)) +
geom_text(data = flights %>% group_by(carrier, origin) %>%
summarize(cancel = 100*mean(cancel, na.rm = T)) %>% ungroup,
aes(factor(carrier), origin, label = round(cancel,1)),
col = "blue", size = 5) +
scale_fill_distiller("Cancel Ratio", palette = "RdYlGn") +
theme_bw()当我执行代码时,错误是
()
: ! Problem while computingcancel= 100 *>: ! Problem while computing(cancel,na.rm = T)`。I错误发生在第1组:承运人=> "9E",原产地= "EWR".
。
有人能告诉我如何解决这个问题吗?非常感谢!
发布于 2022-06-12 16:22:03
使用新列时,不更新原始数据,除非我们使用%<>%而不是%>%。但是,创建两个对象可能更容易。
library(dplyr)
library(ggplot2)
flight1 <- flights %>%
mutate(cancel = 1*(dep_time %>% is.na))
flight2 <- flights1 %>%
group_by(carrier, origin) %>%
summarize(cancel = 100*mean(cancel, na.rm = TRUE), .groups = 'drop')
ggplot(flight1) +
stat_summary_2d(aes(carrier, origin, z = cancel)) +
geom_text(data = flight2, aes(factor(carrier),
origin, label = round(cancel,1)),
col = "blue", size = 5) +
scale_fill_distiller("Cancel Ratio", palette = "RdYlGn") +
theme_bw()https://stackoverflow.com/questions/72593870
复制相似问题