如何使用ggforce等创建一个按照W.E.B. DuBois在R中的风格倾斜45度的堆叠条形图?

zour9fqk  于 2023-02-26  发布在  其他
关注(0)|答案(2)|浏览(124)

鉴于以下数据,我尝试了不同的方法在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)
4uqofj5v

4uqofj5v1#

library(ggplot2)
library(dplyr)
library(tidyr)

# Data
df <- structure(list(Category = c("Government", "Society", "Persons", 
                                  "Property", "Miscellaneous"), Percent = c(0.7, 13.4, 27.1, 48.2, 10.6)), 
                class = "data.frame", row.names = c(NA, -5L)) 

#select incline angle
angle=-pi/4

#create rotation matrix
rotate_matrix = matrix(c(cos(angle),-sin(angle),sin(angle),cos(angle)),ncol = 2,byrow=T)

bar_width=30

#create polygon points, rotate, pivot long
df <- df %>%
  mutate(
    cum_pct = cumsum(Percent),
    x_start = 0,
    x_end = bar_width,
    y_start=lag(cum_pct),
    y_end=cum_pct
  ) %>%
  replace_na(list(y_start=0)) %>%
  rowwise() %>%
  mutate(
    p1_x = (rotate_matrix %*% c(x_start,y_start))[1],
    p1_y = (rotate_matrix %*% c(x_start,y_start))[2],
    p2_x = (rotate_matrix %*% c(x_start,y_end))[1],
    p2_y = (rotate_matrix %*% c(x_start,y_end))[2],
    p4_x = (rotate_matrix %*% c(x_end,y_start))[1],
    p4_y = (rotate_matrix %*% c(x_end,y_start))[2],
    p3_x = (rotate_matrix %*% c(x_end,y_end))[1],
    p3_y = (rotate_matrix %*% c(x_end,y_end))[2]
    ) %>%
  pivot_longer(p1_x:p3_y,names_to = c("point",".value"), names_sep="_") %>%
  arrange(Category,point)

#colors
colors <- c("#E5C2A0", "#D3976A", "#B86F4A", "#A14F3D", "#802E2E")

# Create plot
ggplot(df, aes(x = x,y = y, color = Category, fill = Category))+
  geom_polygon()+
  theme_minimal()+
  scale_color_manual(values = colors)+
  scale_fill_manual(values = colors)+
  labs(title = "W.E.B Du Bois-Style Inclined Stacked Bar")

pkbketx9

pkbketx92#

更简单:

ggplot(df, aes(x = xstart, xend = xend, y = xstart, yend = xend, color = Category)) +
  geom_segment(linewidth = 50) +
  scale_color_manual(values = colors) +
  guides(color = guide_legend(override.aes = list(linewidth = 6) ) ) +
  expand_limits(x= c(-15, 115), y = c(-15,115)) +
  coord_equal()

相关问题