ggplot2改变雷达图中特定线的颜色

时间:2016-07-02 14:05:51

标签: r ggplot2 radar-chart ggproto

我在以下问题中使用了以下示例: 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(strip.text.x = element_text(size = rel(0.8)),
        axis.text.x = element_text(size = rel(0.8)),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank()) +
  xlab("") + ylab("") +
  guides(color = guide_legend(ncol=2)) +
  coord_radar()

print(plot)

如何更改绘图/网格最外边线的颜色(y标签下方的线条)?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

不幸的是,该行使用grid.major参数控制,但不是显式中断。我发现这样做的唯一方法是取消其余的休息:

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(strip.text.x = element_text(size = rel(0.8)),
        axis.text.x = element_text(size = rel(0.8)),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        panel.grid.major.y = element_line(colour = "blue", size = 0.2), #changed
  xlab("") + ylab("") +
  guides(color = guide_legend(ncol=2)) +
  scale_y_continuous(breaks = NULL) + #changed
  coord_radar()

给出了:

enter image description here

不幸的是,你失去了拥有y网格的能力(说实话,如果你没有在极地网格中标记,那就不重要了)。您可以使用element_line

中的颜色参数更改颜色