我想对我的饼图标签和数据片段进行排序。我尝试过对标签进行排序,但是数据片段仍然没有被排序。感谢你的帮助。
![enter image description here](https://istack.dev59.com/nY2Mb.webp)
data = structure(list(Stage = c("Collecting Applications", "Interview Stages",
"Offer Accepted", "Offer in Progress", "To Open"), N = c(17L,
30L, 8L, 2L, 65L), Prop = c(14, 25, 7, 2, 53), Label = c("14%",
"25%", "7%", "2%", "53%")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
data$Stage = factor(data$Stage, ordered = TRUE,
levels = c("To Open", "Collecting Applications", "Interview Stages", "Offer in Progress", "Offer Accepted"))
library(dplyr)
data = data %>%
mutate(end = 2 * pi * cumsum(N)/sum(N),
start = lag(end, default = 0),
middle = 0.5 * (start + end),
hjust = ifelse(middle > pi, 1, 0),
vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))
data = data[order(data$Stage), ]
library(ggforce)
library(ggplot2)
ggplot(data) +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
start = start, end = end, fill = Stage)) +
geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = Label,
hjust = hjust, vjust = vjust)) +
coord_fixed() +
scale_x_continuous(limits = c(-1.5, 1.4), # Adjust so labels are not cut off
name = "", breaks = NULL, labels = NULL) +
scale_y_continuous(limits = c(-1.2, 1.2), # Adjust so labels are not cut off
name = "", breaks = NULL, labels = NULL) +
theme_void() +
labs(title = paste0("Progress for ", sum(data$N), " Positions"),
fill = NULL) +
scale_fill_brewer(palette = "Blues")
cumsum
按正确顺序对数字求和。但是,在factor()
调用中不需要使用ordered = TRUE
- 令人困惑的是,所有因子都有其级别的顺序,ordered = TRUE
是为特殊情况而设计的,例如您可能想要使用<
或者您想在模型中使用特定的对比。 - Gregor Thomasdplyr
语法,并使用data %>% mutate(Stage = factor(Stage, levels = ...)) %>% arrange(Stage) %>% mutate(...)
来保留所有内容。 - Gregor Thomas