尝试在r中创建堆叠条形图

  • 本文关键字:条形图 创建 r ggplot2
  • 更新时间 :
  • 英文 :


[在这里输入图像描述][1]刚开始编码并尝试创建堆叠条形图。我想要的值exp_pupil对于每个城市在y轴上,y轴上的5个城市,每个人口的百分比构成代表在每一个酒吧和一个关键。

下面是目前为止的代码:
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()

这是我的数据:

> 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

你可以试试。

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()

最新更新