首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将col图转换为金字塔格式

将col图转换为金字塔格式
EN

Stack Overflow用户
提问于 2021-01-28 12:06:46
回答 1查看 24关注 0票数 0

这种类型的绘图在ggplot中是可行的吗?我见过这样的例子:

http://alburez.me/2018-03-20-Population-pyramids-in-R-for-beginners/

但实际上我想要更简单的东西,像这样:

在ggplot中这是可能的吗?我的第一个直觉是创建一个柱状图/柱状图,但不能像上面的附图那样使柱状图居中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-28 15:07:55

当然,这可以通过ggplot2来实现。一种选择是像这样使用geom_rect

代码语言:javascript
复制
library(ggplot2)

dat <- data.frame(
  name = factor(c("Gen Z", "Millenials", "Gen X")),
  name_y = c(3:1),
  pct = c(.3, .51, .19)
)

midpoint <- max(dat$pct) / 2
ggplot(dat) + 
  geom_rect(aes(xmin = midpoint - pct / 2, xmax = midpoint + pct / 2,
                ymin = name_y - .45, ymax = name_y + .45),
            fill = "steelblue", color = "white") +
  geom_text(aes(x = midpoint, y = name_y, label = scales::percent(pct)), color = "white", fontface = "bold") +
  scale_y_continuous(breaks = unique(dat$name_y), labels = unique(dat$name)) +
  theme_minimal() +
  labs(x = NULL, y = NULL) +
  theme(axis.line.x = element_blank(), axis.text.x = element_blank(),
        panel.grid = element_blank())

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65930900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档