geom_area 默认绘制堆叠区域

M--*_*M-- 6 r ggplot2

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))
Run Code Online (Sandbox Code Playgroud)

情节最

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

但是再看看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)
Run Code Online (Sandbox Code Playgroud)

Uma*_*mao 6

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

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

你可以清楚地看到这一点,如果你分离出来,prct.DCIAfacet_grid()

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

在此处输入图片说明

这仅仅是因为position = "stack" 是 中的默认参数 geom_area

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

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

参见 的默认参数 geom_lineposition = "identity"

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

修复: 如果您使用,position = position_dodge()您可以看到它们恢复为折线图,红色区域绘制在蓝色区域后面:

  x %>% gather(prct.DCIA, Vol, -Time) %>% ggplot(aes(x=Time, y=Vol)) +
  geom_area(aes(fill=prct.DCIA), position = position_dodge())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

您甚至可以设置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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明