gpt4 book ai didi

r - geom_area 默认绘制堆叠区域

转载 作者:行者123 更新时间:2023-12-01 00:15:00 25 4
gpt4 key购买 nike

我正在使用 geom_area绘制一个非常简单的数据集。使用 geom_line 绘图时一切正常,但是当我切换到 geom_area 时绘制更高的值。查看图表,将是代表我的问题的最佳方式:

require(tidyverse)

x <- structure(list(Time = 0:40, X15.DCIA = c(0, 1, 0.5, 0, 2, 2.5,
1, 0.5, 0, 1, 1.5, 1, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 3,
5, 7, 6.5, 5.5, 4, 3, 2, 1.5, 1, 0.25, 0, 0, 0, 0, 0, 0, 0),
X100.DCIA = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1.5, 7, 8, 7.5, 6.5, 5, 3.5, 2.25,
1.75, 1.1, 0.4, 0.1, 0, 0, 0, 0, 0, 0)),
class = "data.frame", row.names = c(NA,-41L))

x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) +
geom_line(aes(color=prct.DCIA))


x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) +
geom_area(aes(fill=prct.DCIA))

plotst

这是我所期望的(我的数据的线图)。

但是再看 geom_area你看 100DCIA已跃升至 15。

我对 更感兴趣说明 而不是修复或解决方法。

注:

这可以是一种解决方法:
x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) + 
geom_polygon(aes(fill=prct.DCIA, alpha=0.5)) + guides(alpha=FALSE)

最佳答案

说明:
您的地块相互叠加。

您在 geom_area 中的红线后面看到的值图是 geom_line 中红线和蓝线的值的总和图形。

如果你把 prct.DCIA 分开,你可以清楚地看到这一点。与 facet_grid() :

x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) +
geom_area(aes(fill=prct.DCIA)) + facet_grid(.~prct.DCIA)

enter image description here

这仅仅是因为 position = "stack" is the default argument in geom_area :

geom_area(mapping = NULL, data = NULL, stat = "identity",
position = "stack", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...)


人们可能会认为这是因为人们使用 geom_area因为他们想在图表上显示整个区域,而不是在某些线条下填充。通常条形或面积可能代表某物的计数,或者填充的区域代表某物,而点或线可能代表点估计,线或点上方或下方的区域没有意义。

参见 the default argument for geom_lineposition = "identity" .

geom_line(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...)


修复:
如果您使用 position = position_dodge()您可以看到它们恢复为折线图,红色区域绘制在蓝色区域后面:
  x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) +
geom_area(aes(fill=prct.DCIA), position = position_dodge())

enter image description here

您甚至可以设置 alpha < 1 并清楚地看到这一点:
x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) +
geom_area(aes(fill=prct.DCIA), position = position_dodge(), alpha = 0.5)

enter image description here

关于r - geom_area 默认绘制堆叠区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53549657/

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