[enter image description here][1]New to coding and trying to create a stacked barchart. I want to have the values of exp_pupil for each city on the y axis, the 5 cities on the Y axis, and the percent makeup of each demographic represented in each bar with a key.
Here's my code so far:
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()
Here's my data too:
> 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"))
This is an example of what I want it to look like: [1]: https://i.stack.imgur.com/XC8yq.png