使用ggplot2的雷达图中的未闭合线

时间:2018-07-26 17:16:55

标签: r ggplot2

我可以使用coord_polar()中的函数ggplot2生成漂亮的极坐标/雷达图。它们是图形化地汇总数据的一种非常说明性的方式。但是,当跟踪一条线并为该线定义的区域着色时,我发现它并不完全封闭。也就是说,图表中包含的第一个点与最后一个点之间存在一个差距。为了说明这一点,让我们使用我的数据集:

dataset <- structure(list(Comparison = c(NA, NA, 8.74089490114464, NA, -21.4860528264626, 
-39.2776554202867, -17.6623719989853, -24.6866200381506, -14.7775465072786, 
-7.63468366388391, -11.085710605963, 19.9485934599457, -4.60660415817366, 
-19.826927971494, NA, -3.27723649247121), Tooth = c("UI1", "LI1", 
"UI2", "LI2", "UC", "LC", "UP3", "LP3", "UP4", "LP4", "UM1", 
"LM1", "UM2", "LM2", "UM3", "LM3"), group = c("a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"
)), .Names = c("Comparison", "Tooth", "group"), class = "data.frame", row.names = c("UI1", 
"LI1", "UI2", "LI2", "UC", "LC", "UP3", "LP3", "UP4", "LP4", 
"UM1", "LM1", "UM2", "LM2", "UM3", "LM3"))

我使用了此ggplot代码:

order_teeth <- c("UP4", "UM1", "UM2", "UM3", "LM3", "LM2", "LM1", "LP4", "LP3", "LC", "LI2", "LI1", "UI1", "UI2", "UC", "UP3")

ggplot(data=na.omit(dataset), aes(x=Tooth, y=Comparison, group=group)) + 
  ylab("%diff") + 
  ylim(25, -40) +
  xlab("GroupA vs GroupB") + 
  scale_x_discrete(limits=c(order_teeth)) +
  geom_area(alpha=0.2, position = position_identity(), color = "#7CAE00", fill = "#7CAE00") +
  geom_hline(aes(group=group, yintercept=0), lwd=1, lty=5, alpha=0.8) + 
  geom_hline(aes(group=group, yintercept=mean(dataset$Comparison, na.rm = TRUE)), lwd=0.75, lty=2, alpha=0.8, color="darkgreen") +
  geom_point(fill = "#7CAE00", size=2.5, shape= 21, color = "black") + 
  coord_polar() + 
  theme(legend.key = element_blank(), panel.background = element_blank(), panel.grid.major = element_line(colour = "gray92"))

如您所见,我首先定义了我希望类别(在本例中为牙齿)出现在雷达图周围的确切顺序。稍后,我运行ggplot图。我创建了这个奇妙的情节:

enter image description here

但是,我需要解决一个重要问题:关闭该行并为下一张图像的红色圆圈中突出显示的区域着色。最重要的是,我之所以这样说是因为我在其他地方读过,牙齿的位置(深色箭头)必须保持不动

enter image description here

如何修改代码以解决这些问题?

0 个答案:

没有答案