使用ggplot2生成雷达图

5
为了方便重现,您可以在此处下载数据。其结构如下:
> str(data)
'data.frame':   30 obs. of  4 variables:
 $ Count: num  -15.26 NaN NaN -7.17 -49.37 ...
 $ X1   : Factor w/ 1 level "Mean": 1 1 1 1 1 1 1 1 1 1 ...
 $ X2   : Factor w/ 10 levels "DC1","DC10","DC2",..: 1 1 1 3 3 3 4 4 4 5 ...
 $ X3   : Factor w/ 3 levels "SAPvsSH","SAPvsTD6",..: 1 2 3 1 2 3 1 2 3 1 ...

我运行了这个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))

使用此结果:

enter image description here

这张图表表示SH与TD6相对于SAP(水平黑线,分别用红色和绿色表示)以及TD6相对于SH(在这种情况下也由水平黑线表示,但现在是蓝色)的差异百分比。我使用了十个变量:DC1:DC10我想把这张图表转换成雷达图。我尝试使用ggradarggRadar,但没有成功。像这样的东西会很棒:

enter image description here

水平的黑线应该是完美的圆形,就像前一张图片中红线和蓝线之间放置的圆形一样。理想情况下,DC1应该朝北放置,顺时针旋转。

有任何想法或建议吗?


3
g + coord_polar() 会将 ggplot 对象 g 中的坐标轴转换为极坐标轴。 - DJack
我已经使用ggplot工作很长时间了,但从未见过那个选项!太棒了!谢谢@DJack - antecessor
1个回答

6

感谢 @DJack,我在这里发布添加了 + coord_polar() 的结果:

enter image description here

这是最终代码:

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

我很好奇为什么这些圆的半径不成比例。我不知道我错过了什么。 - Urvah Shabbir
1
为什么这些线不是直的?有没有办法修复它? - Holger Brandl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接