如何在雷达图表中放置Y轴值

时间:2018-05-03 08:32:43

标签: r ggplot2 radar-chart

我在以下问题中使用了以下示例:http://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.html

mtcarsscaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))
mtcarsscaled$model <- rownames(mtcars)
mtcarsmelted <- reshape2::melt(mtcarsscaled)

coord_radar <- function (theta = "x", start = 0, direction = 1) 
{
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x") 
    "y"
  else "x"
  ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
          direction = sign(direction),
          is_linear = function(coord) TRUE)
}

plot <- ggplot(mtcarsmelted, aes(x = variable, y = value)) +
  geom_polygon(aes(group = model, color = model), fill = NA, size = 2, show.legend = FALSE) +
  geom_line(aes(group = model, color = model), size = 2) +
  theme_bw()+
  theme(axis.text.x = element_text(colour="black",size=10), axis.text.y=element_text(size=14))+
  xlab("") + ylab("") +
  guides(color = guide_legend(ncol=2)) +
  coord_radar()

print(plot)

enter image description here

如何将y轴的值移动到雷达图上,以便它们与主要的y轴网格线对齐并摆脱图表周围的黑框?

1 个答案:

答案 0 :(得分:1)

您可以将y轴值添加为带注释的文字,并通过更改panel.border中的theme(...)参数来删除黑匣子。

ggplot(mtcarsmelted, aes(x = variable, y = value)) +
  geom_polygon(aes(group = model, color = model), 
               fill = NA, size = 2, show.legend = FALSE) +
  geom_line(aes(group = model, color = model), size = 2) +
  xlab("") + ylab("") +
  guides(color = guide_legend(ncol = 2)) +
  coord_radar() +
  theme_bw()+

  # annotate
  annotate("text", x = 0, 
           y = seq(0, 1, 0.25), 
           label = seq(0, 1, 0.25)) +

  # remove original y-axis text / ticks & black border
  theme(axis.text.x = element_text(colour="black", size=10), 
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.border = element_blank()) 

plot

旁注:同一图表中的模型太多,以便于阅读。如果你这样做是为了练习,那没关系。但是如果你打算用它来与他人交流,你可能希望面对情节,以便每个情节中只显示几个模型,或选择一个仅强调一些感兴趣模型的不同颜色方案。