gpt4 book ai didi

r - 如何在ggplot2中制作累积层图

转载 作者:行者123 更新时间:2023-12-01 23:37:31 26 4
gpt4 key购买 nike

我有一个类似于下面示例的数据:

library(dplyr)
nycflights13::flights %>%
mutate(date = as.Date(paste(day, month, year, sep = '-'), format = '%d-%m-%Y')) %>%
select(date, carrier, distance)

现在我需要构建一个包含每天距离总和的图,其中后续层将引用不同的承运人。我的意思是类似于

ggplot(diamonds, aes(x = price, fill = cut)) + geom_area(stat = "bin") 

但使用 sum 作为 stat

我试过

nycflights13::flights %>% 
mutate(date = as.Date(paste(day, month, year, sep = '-'), format = '%d-%m-%Y')) %>%
select(date, carrier, distance) %>%
ggplot() +
geom_area(aes(date, distance, fill = carrier, group = carrier), stat = 'sum')

但它并没有成功,导致

Error in f(...) : Aesthetics can not vary with a ribbon

使用 geom_bar 非常简单,但是有什么想法可以制作堆叠的 geom_area 图吗?

最佳答案

library(dplyr)
nycflights13::flights %>%
mutate(date = as.Date(paste(day, month, year, sep = '-'),
format = '%d-%m-%Y')) %>%
select(date, carrier, distance) %>%
group_by(date, carrier) %>%
summarise(distance = sum(distance)) %>%
ggplot() +
geom_area(aes(date, distance, fill = carrier,
group = carrier), stat = 'identity')

应该可以解决问题。 enter image description here

关于r - 如何在ggplot2中制作累积层图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51748708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com