使用R tidyverse,ggplot,geom_area的面积图

西尔维亚·罗德里格斯(Sylvia Rodriguez)

我正在使用以下代码使用tidyverseggplot函数和生成面积图geom_area

 library(tidyverse)
 set.seed(12345)
 df1 <- data.frame(a = c(2000:2020), b = rnorm(21,25,4), c = rep("x", 21))
 df2 <- data.frame(a = c(2006:2020), b = rnorm(15,40,7), c = rep("y", 15))
 df3 <- data.frame(a = c(2017:2020), b = rnorm(4,20,3), c = rep("z", 4))
 df <- rbind(df1, df2, df3)
 ggplot(df, aes(x=a, y=b, fill=c)) + geom_area() + theme(legend.position = "none")

这种方法有效,但是底部有两个空心三角形,我想分别用绿色和蓝色填充。我以为可以通过更改如下代码来解决此问题:

 df1 <- data.frame(a = c(2000:2020), b = rnorm(21,25,4), c = rep("x", 21))
 df1 <- rbind(c("1999", "0","x"), df1)
 df2 <- data.frame(a = c(2006:2020), b = rnorm(15,40,7), c = rep("y", 15))
 df2 <- rbind(c("2005", "0","y"), df2)
 df3 <- data.frame(a = c(2017:2020), b = rnorm(4,20,3), c = rep("z", 4))
 df3 <- rbind(c("2016", "0","z"), df3)
 df <- rbind(df1, df2, df3)
 ggplot(df, aes(x=a, y=b, fill=c)) + geom_area(position=stack) + theme(legend.position = "none")

但是现在,绘图已不再起作用。这篇文章对我没有帮助。有人可以帮忙吗?

r2evans
library(dplyr)
df2 <- group_by(df, c) %>%
  summarize(a = min(a) - 1) %>%
  mutate(b = 0) %>%
  bind_rows(df) %>%
  arrange(a,c)
ggplot(df2, aes(x=a, y=b, fill=c)) +
  geom_area() +
  theme(legend.position = "none")

ggplot2填充有孔的面积图

如果第一个小组的晋升有问题,那么我们可以简单地将其删除:

df2 <- group_by(df, c) %>%
  summarize(a = min(a) - 1) %>%
  mutate(b = 0) %>%
  bind_rows(df) %>%
  arrange(a,c) %>%
  slice(-1)
ggplot(df2, aes(x=a, y=b, fill=c)) +
  geom_area() +
  theme(legend.position = "none")

与第一组相同的情节没有增加

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

R用ggplot geom_area排列堆积区域的顺序

来自分类Dev

R:堆叠 geom_area 图显示空白多边形

来自分类Dev

如何使用 ggplot 使我的面积图堆叠?

来自分类Dev

R-ggplot2:如果将限制定义为data.frame中的最大值和最小值,则geom_area会丢失

来自分类Dev

geom_area返回空图

来自分类Dev

geom_area不堆叠(ggplot)

来自分类Dev

堆叠的ggplot2 geom_area重新运行一个空图

来自分类Dev

使用ggplot2的堆积面积图的颜色问题

来自分类Dev

R ggplot-一起使用geom_box图和geom_ribbon

来自分类Dev

如何使用单独的列中的数据生成堆叠的geom_area(fill)?

来自分类Dev

使用geom_area未正确绘制的区域(某些区域具有偏移)

来自分类Dev

ggplot2因子x变量中断geom_area

来自分类Dev

ggplot geom_area中的未填充区域

来自分类Dev

ggplot2:阶乘x轴的geom_area

来自分类Dev

ggplot2 geom_area 产生意外输出

来自分类Dev

ggplot2:消除由 geom_area 引入的间隙?

来自分类Dev

在R中使用ggplot的意大利面条图?

来自分类Dev

在R中使用ggplot创建图

来自分类Dev

ggplot 使用 R 分组的条形图

来自分类Dev

R-ggplot2-geom_area-删除geom_line下颜色填充的中断

来自分类Dev

ggplot geom_area 分布与相同的 geom_histogram 非常不同

来自分类Dev

如何在R中使用ggplot / geom_bar在条形图顶部的数据集中添加自定义标签?

来自分类Dev

基数R中缺少值的面积图

来自分类Dev

具有最小值和最大值的 geom_area 图

来自分类Dev

使用画布创建极地面积图

来自分类Dev

为geom_area(ggplot2)添加缺少的data.frame值

来自分类Dev

填写ggplot2中的线图下方的区域:geom_area()

来自分类Dev

ggplot2:具有阶乘x轴的geom_area

来自分类Dev

添加geom_area后在ggplot2中添加图例

Related 相关文章

热门标签

归档