0

[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

  • 1
    Can you use `dput(ed_proj)` to allow folks to copy and paste the data you're working with? – setty Oct 07 '21 at 16:27
  • @setty I'm not sure how to do that- I just copied and pasted it above my data. Does that work? Sorry- very new to this – user17099810 Oct 07 '21 at 16:34
  • 1
    No problem. If you run dput(ed_proj) in the console, it will print out something like `structure(list(...))`. Copy and paste the output from the console. That will allow others to run that code and recreate your data frame (or tibble in this case). For more info on dput, checkout: https://stackoverflow.com/questions/49994249/example-of-using-dput – setty Oct 07 '21 at 16:38
  • @setty - I edited it. You should be able to see it now. Let me know! – user17099810 Oct 07 '21 at 17:05
  • I left an example! I need the y axis to be exp_pupil so the bars are different heights – user17099810 Oct 07 '21 at 21:26
  • Thanks, let me know if the code I've added below is what you're after. – setty Oct 07 '21 at 22:59
  • It looks great! The only thing is that all the groups are an equal size- I wanted them to reflect the percent of the racial group. Is that possible? – user17099810 Oct 07 '21 at 23:35
  • That should do it – setty Oct 07 '21 at 23:54

1 Answers1

0

Here's something to try.

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

setty
  • 425
  • 3
  • 18