[英]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.