我正在使用 ggplot2 创建一个堆叠面积图,显示多个不同研究站随时间变化的足迹(面积)。我想要看起来像下图的东西,但 y 轴上有 Area 并且由不同的研究站着色:
(来源:r-graph-gallery.com)
我尝试过类似帖子的元素,但无法正常工作。
Trouble with ggplot in R - "Error in f(...) : Aesthetics can not vary with a ribbon"
Getting a stacked area plot in R
https://www.r-graph-gallery.com/136-stacked-area-chart/
我提供了数据的 .csv 子集 here .
下面是我正在使用的代码。
fp <- read.csv("fp.csv")
fp$Year <- as.numeric(rep(fp$Year)) #change Year to numeric
p2 <- fp %>%
filter(Exist == 1) %>% # Select only existing structures
group_by(Year, Station) %>%
summarise(Sum_Area = sum(Area)) %>%
arrange(desc(Year)) %>%
ggplot(aes(x = Year, y = Sum_Area, fill = Sum_Area)) +
geom_area(stat = "identity", position = "stack")
p2
我总是收到错误消息:f(...) 错误:色带不能改变美学
最佳答案
如果您的填充变量是整数或数字变量而不是字符或因子变量,就会发生这种情况。
重现错误
airquality %>%
ggplot(aes(x = Day, y = Ozone, fill = Month)) +
geom_area()
#Error: Aesthetics can not vary with a ribbon
修正错误
airquality %>%
mutate(Month = as.character(Month)) %>%
ggplot(aes(x = Day, y = Ozone, fill = Month)) +
geom_area()
这当然是一个无用的情节,只是为了说明错误。
关于r - 如何修复 f(...) : Aesthetics can not vary with a ribbon 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333161/