我正在尝试使用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))
任何帮助都将受到欢迎! 非常感谢
答案 0 :(得分:3)
coord_polar
不能很好地处理这种情况的原因是,因为您的第一个(最后一个)(末尾)的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值,它们位于原始多边形的第一个点与最后一个点之间。 / p>
现在有数据要弯曲,我们可以绘制一个漂亮的弯曲径向图:
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))
请注意,我尚未对此几何图形进行广泛的测试,但是它似乎很适合解决此问题以及类似的其他问题。