鉴于以下数据,我尝试了不同的方法在R没有成功。有人能帮助吗?
structure(list(Category = c("Government", "Society", "Persons",
"Property", "Miscellaneous"), Percent = c(0.7, 13.4, 27.6, 48.2,
10.6)), class = "data.frame", row.names = c(NA, -5L))
这是我尝试过的代码:
library(ggplot2)
library(ggforce)
# Data
df <- structure(list(Category = c("Government", "Society", "Persons",
"Property", "Miscellaneous"), Percent = c(0.7, 13.4, 27.6, 48.2, 10.6)),
class = "data.frame", row.names = c(NA, -5L))
# Calculate endpoints for the lozenge shape
df$xend <- c(0, cumsum(df$Percent)[-length(df$Percent)])
df$xstart <- df$xend + df$Percent
df$y <- 1
# Set colors for the stacked bars
colors <- c("#E5C2A0", "#D3976A", "#B86F4A", "#A14F3D", "#802E2E")
# Create plot
ggplot(df, aes(x = y, y = xstart, fill = Category, group = Category)) +
geom_polygon(aes(x = y, y = xstart, group = Category), fill = colors[1], color = NA) +
geom_polygon(aes(x = y, y = xend, group = Category), fill = colors[2], color = NA) +
geom_polygon(aes(x = y, y = xend, group = Category), fill = colors[3], color = NA) +
geom_polygon(aes(x = y, y = xend, group = Category), fill = colors[4], color = NA) +
geom_polygon(aes(x = y, y = xend, group = Category), fill = colors[5], color = NA) +
scale_fill_manual(values = rev(colors)) +
coord_flip() +
theme_void() +
theme(legend.position = "none") +
geom_text(aes(x = 0.5, y = cumsum(Percent) - Percent/2, label = paste0(Percent, "%")),
color = "white", size = 4, fontface = "bold", angle = 90, hjust = 0.5, vjust = 0.5)
2条答案
按热度按时间4uqofj5v1#
pkbketx92#
更简单: