繁体   English   中英

如何用ggplot2制作弯曲雷达图?

[英]How to make a curved radar plot with ggplot2?

我正在尝试用ggplot2制作雷达图。 我使用了geom_polygon和coord_polar函数来创建一个封闭的曲线图。 问题是在最后一点和第一点之间,线不弯曲,与其他线相反。 我想知道为什么会这样,我想修改线条以使其弯曲。

我看过coord_polar函数,但我找不到任何解决方案......

这是我的代码:

dput(vsg_emo_nbRC)

structure(list(variable = structure(c(2L, 3L, 4L, 5L, 6L, 2L, 
3L, 4L, 5L, 6L), .Label = c("", "Neutre", "Colère", "Embarras", 
"Fierté", "Surprise"), class = "factor"), Groupe = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("DS", "VS"), class = "factor"), 
    moy_DS = c(2.22, 3.11, 2.89, 3.33, 2.67, 3.36, 3.88, 3.48, 
    4, 3.6), std_err_DS = c(1.72, 1.17, 1.54, 1.12, 0.71, 0.95, 
    0.33, 0.65, 0, 0.71), IC_upper_DS = c(2.6, 3.37, 3.22, 3.58, 
    2.82, 3.43, 3.91, 3.53, 4, 3.66), IC_lower_DS = c(1.85, 2.86, 
    2.55, 3.09, 2.51, 3.29, 3.85, 3.43, 4, 3.54)), row.names = c(1L, 
2L, 3L, 4L, 5L, 30L, 31L, 32L, 33L, 34L), class = "data.frame")

vsg_emo_nbRC %>%
  ggplot(aes(x = variable, y = moy_DS, colour = Groupe, group = Groupe, linetype = Groupe)) +
  geom_polygon(aes(y = IC_upper_DS), fill = "grey50", alpha = 0.2, linetype= "dotted") +
  geom_polygon(aes(y = IC_lower_DS), fill = "grey99", alpha = 0.2, linetype="dotted") +
  geom_polygon(fill = NA) +
  theme_light() +
  theme(panel.grid.minor = element_blank()) + 
  coord_polar() +
  scale_colour_manual(values = c("#d8b365", "#5ab4ac")) +
  scale_linetype_manual(values = c(1,1))  +
  labs(x = "", y = "", title = "Performances moyennes de reconnaissance d'émotions dans la tâche visage") +
  expand_limits(x=c(0,4), y=c(0,4)) +
  geom_text(aes(label=moy_DS)) +
  scale_y_continuous(limits = c(0,4))

欢迎任何帮助! 非常感谢

在此输入图像描述

coord_polar不能很好地处理这种情况的原因是因为在你的第一个和最后一个(Surprise&Neutre)x位置之间存在一个不可见的x轴边界,因为没有数据可以弯曲。

有一个解决方案可以构建,但它将涉及建立一个新的geom所以我们去。 首先,我们将构建我们实际使用的函数:

geom_polarpoly <- function (mapping = NULL, data = NULL, stat = "identity", 
                            position = "identity", rule = "evenodd", ..., 
                            na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) 
{
  layer(data = data, mapping = mapping, stat = stat, geom = GeomPolarPoly, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(na.rm = na.rm, rule = rule, ...))
}

这与geom_poly()几乎完全相同,但它将geom设置为我们接下来要构建的ggproto对象:

GeomPolarPoly <- ggproto(
  "GeomPolarPoly", 
  GeomPolygon,
  draw_panel = function(data, panel_params, coord, rule = "evenodd", self) {
    df <- split(data, data$group)
    df <- lapply(df, function(dat) {
      dummy <- rbind(head(dat, 1), tail(dat, 1))
      dummy$x <- panel_params$theta.range
      dummy$y <- mean(dummy$y)
      rbind(dummy[1, ],
            dat,
            dummy[2, ])
    })
    data <- do.call(rbind, df)

    ggproto_parent(GeomPolygon, self)$draw_panel(data = data, 
                                                 panel_params = panel_params, 
                                                 coord = coord, 
                                                 rule = rule)
  })

这个ggproto对象的作用是将GeomPolygon所有内容复制到面板绘图代码中。 与原始面板绘图代码的不同之处在于,它现在将在x轴的两个极端处放置两个额外的点,并且这两个点都是在原始多边形的第一个点和最后一个点之间的y值。

现在有数据需要弯曲,我们可以制作一个很好的弯曲径向图:

vsg_emo_nbRC %>%
  ggplot(aes(x = variable, y = moy_DS, colour = Groupe, group = Groupe, linetype = Groupe)) +
  geom_polarpoly(aes(y = IC_upper_DS), fill = "grey50", alpha = 0.2, linetype= "dotted") +
  geom_polarpoly(aes(y = IC_lower_DS), fill = "grey99", alpha = 0.2, linetype="dotted") +
  geom_polarpoly(fill = NA) +
  theme_light() +
  theme(panel.grid.minor = element_blank()) + 
  coord_polar() +
  scale_colour_manual(values = c("#d8b365", "#5ab4ac")) +
  scale_linetype_manual(values = c(1,1))  +
  labs(x = "", y = "", title = "Performances moyennes de reconnaissance d'émotions dans la tâche visage") +
  expand_limits(x=c(0,4), y=c(0,4)) +
  geom_text(aes(label=moy_DS)) +
  scale_y_continuous(limits = c(0,4))

在此输入图像描述

请注意,我没有广泛测试过这个geom,但它似乎符合这个问题的法案,以及其他类似的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM