首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在R中创建堆叠条形图

尝试在R中创建堆叠条形图
EN

Stack Overflow用户
提问于 2021-10-07 16:00:16
回答 1查看 48关注 0票数 0

输入要编码并尝试创建堆叠条形图的图像描述hereNew。我想要Y轴上每个城市的exp_pupil值,Y轴上的5个城市,以及每个条形图中每个人口构成的百分比。

到目前为止,我的代码如下:

代码语言:javascript
复制
ed_proj %>% group_by(city) %>%.  
  summarize(n=n()) %>%.  
  ggplot(aes(x=city)) +
  ggplot(aes(y=exp_pupil)) + #makes the data
  geom_col(aes(fill= "percent"), show.legend=F) + # color bars by pres
  geom_text(aes(label="group"), size=2, vjust=0) + # add labels to bars
  labs(x="City", y="Expenditure per Pupil", title="") +
  theme_light()

下面是我的数据:

代码语言:javascript
复制
> dput(ed_proj)
structure(list(city = c("Chelsea", "Chelsea", "Chelsea", "Chelsea", 
"Chelsea", "Roxbury", "Roxbury", "Roxbury", "Roxbury", "Roxbury", 
"Medford", "Medford", "Medford", "Medford", "Medford", "Newton", 
"Newton", "Newton", "Newton", "Newton", "Brookline", "Brookline", 
"Brookline", "Brookline", "Brookline"), red_data = c("red", "red", 
"red", "red", "red", "red", "red", "red", "red", "red", "yellow", 
"yellow", "yellow", "yellow", "yellow", "blue", "blue", "blue", 
"blue", "blue", "green", "green", "green", "green", "green"), 
    group = c("white", "black", "asian", "hispanic", "other", 
    "white", "black", "asian", "hispanic", "other", "white", 
    "black", "asian", "hispanic", "other", "white", "black", 
    "asian", "hispanic", "other", "white", "black", "asian", 
    "hispanic", "other"), percent = c(20, 3, 6, 67, 3, 6, 1, 
    55, 30, 8, 70, 10, 10, 7, 3, 77, 15, 3, 4, 1, 72, 17, 3, 
    7, 1), exp_pupil = c(16162.26, 16162.26, 16162.26, 16162.26, 
    16162.26, 17983.41, 17983.41, 17983.41, 17983.41, 17983.41, 
    18321.74, 18321.74, 18321.74, 18321.74, 18321.74, 20219.94, 
    20219.94, 20219.94, 20219.94, 20219.94, 20542.82, 20542.82, 
    20542.82, 20542.82, 20542.82)), row.names = c(NA, -25L), spec = structure(list(
    cols = list(city = structure(list(), class = c("collector_character", 
    "collector")), red_data = structure(list(), class = c("collector_character", 
    "collector")), group = structure(list(), class = c("collector_character", 
    "collector")), percent = structure(list(), class = c("collector_double", 
    "collector")), exp_pupil = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x7fe2d6c419c0>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

这是我希望它看起来是什么样子的示例: 1:https://i.stack.imgur.com/XC8yq.png

EN

回答 1

Stack Overflow用户

发布于 2021-10-07 18:59:17

这里有一些可以尝试的东西。

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

ed_proj <- structure(list(city = c("Chelsea", "Chelsea", "Chelsea", "Chelsea", 
"Chelsea", "Roxbury", "Roxbury", "Roxbury", "Roxbury", "Roxbury", 
"Medford", "Medford", "Medford", "Medford", "Medford", "Newton", 
"Newton", "Newton", "Newton", "Newton", "Brookline", "Brookline", 
"Brookline", "Brookline", "Brookline"), red_data = c("red", "red", 
"red", "red", "red", "red", "red", "red", "red", "red", "yellow", 
"yellow", "yellow", "yellow", "yellow", "blue", "blue", "blue", 
"blue", "blue", "green", "green", "green", "green", "green"), 
    group = c("white", "black", "asian", "hispanic", "other", 
    "white", "black", "asian", "hispanic", "other", "white", 
    "black", "asian", "hispanic", "other", "white", "black", 
    "asian", "hispanic", "other", "white", "black", "asian", 
    "hispanic", "other"), percent = c(20, 3, 6, 67, 3, 6, 1, 
    55, 30, 8, 70, 10, 10, 7, 3, 77, 15, 3, 4, 1, 72, 17, 3, 
    7, 1), exp_pupil = c(16162.26, 16162.26, 16162.26, 16162.26, 
    16162.26, 17983.41, 17983.41, 17983.41, 17983.41, 17983.41, 
    18321.74, 18321.74, 18321.74, 18321.74, 18321.74, 20219.94, 
    20219.94, 20219.94, 20219.94, 20219.94, 20542.82, 20542.82, 
    20542.82, 20542.82, 20542.82)), row.names = c(NA, -25L), spec = structure(list(
    cols = list(city = structure(list(), class = c("collector_character", 
    "collector")), red_data = structure(list(), class = c("collector_character", 
    "collector")), group = structure(list(), class = c("collector_character", 
    "collector")), percent = structure(list(), class = c("collector_double", 
    "collector")), exp_pupil = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

ed_proj <- ed_proj %>%
  group_by(city) %>%
  arrange(desc(group)) %>%
  mutate(exp_pupil_percent = exp_pupil * (percent/100),
         label_y = cumsum(exp_pupil_percent)) # create a y value for the labels for each city

ed_proj %>%
  ggplot(aes(x=city, y = exp_pupil_percent)) +
  geom_col(aes(fill= group), show.legend=F) + # color bars by pres
  geom_text(aes(y = label_y, label=group), size=2) + # add labels to bars
  labs(x="City", y="Expenditure per Pupil", title="") +
  theme_light()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69484338

复制
相关文章

相似问题

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