Color areas in a radar chart using geom_area() in

2019-05-10 06:48发布

Before reading any longer, I suggest you to download and see the original codes in this question posted in this forum.

I run this ggplot code:

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) +

And got this plot:

enter image description here

As you may imagine, something's wrong with the code. Blue group seems to be colored properly. I would like to color all the groups, taking as reference the black-slashed ring, which represent 0.

How can I implement this?

标签: r ggplot2
1条回答
狗以群分
2楼-- · 2019-05-10 07:40

geom_area() has a default position argument of stack. This stack each area over the others, creating weird results in this case.

Simply change the position to 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).

Created on 2018-05-15 by the reprex package (v0.2.0).

查看更多
登录 后发表回答