在 ggplot2 中使用 geom_area() 为雷达图中的区域着色

ant*_*sor 4 r ggplot2

在继续阅读之前,我建议您下载并查看本论坛发布的此问题中的原始代码。

我运行这个 ggplot 代码:

ggplot(data=data,  aes(x=X2, y=Count, group=X3, colour=X3)) + 
  geom_point(size=5) + 
  geom_line() + 
  xlab("Decils") + 
  ylab("% difference in nº Pk") + 
  ylim(-50,25) + ggtitle("CL")  + 
  geom_hline(aes(yintercept=0), lwd=1, lty=2) + 
  scale_x_discrete(limits=c(orden_deciles)) +
  coord_polar() +
  geom_area(aes(color=X3, fill=X3), alpha=0.2) +
Run Code Online (Sandbox Code Playgroud)

并得到了这个情节:

在此处输入图片说明

正如您想象的那样,代码有问题。蓝色组似乎正确着色。我想为所有组着色,以黑色斜线环为参考,代表 0。

我该如何实施?

GGa*_*mba 5

geom_area()有一个默认position参数stack。这将每个区域叠加在其他区域上,在这种情况下会产生奇怪的结果。

只需将位置更改为identity

library(ggplot2)

## This was not given, I supposed it's in this form
orden_deciles <- paste0('DC', 1:10)

ggplot(data=data,  aes(x=X2, y=Count, group=X3, colour=X3)) + 
  geom_point(size=5) + 
  geom_line() + 
  xlab("Decils") + 
  ylab("% difference in nº Pk") + 
  ylim(-50,25) + ggtitle("CL")  + 
  geom_hline(aes(yintercept=0), lwd=1, lty=2) + 
  scale_x_discrete(limits=c(orden_deciles)) +
  geom_area(aes(fill=X3), alpha=0.2, position = position_identity()) +
  coord_polar()
#> Warning: Removed 5 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_path).
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.0)于 2018 年 5 月 15 日创建。