0

I am learning ggplot2 and I am trying to make a chart with multiple boxplots, one for each age group along the x-axis, showing information about the distribution of total population for each age group along the y-axis. I am following a prompt to create a specific plot (seen below).

Age groups each have a value, 0 (all age groups), to 18 (the highest age group) in the AGEGRP column. There's also a column for TOT_POP (total population). The dataset looks like this:

samplePopulationHead

And this is the chart I am trying to create. I think the trick will be to factor the AGEGRP values.

Boxplot goal

Here's the code I tried for the plot, but it fails. Would someone please point me in the right direction? Thank you,

library(tidyverse)

popSample <- read.csv("./datafiles/cc-est2018-alldata.csv") %>%
  select("STNAME", "CTYNAME", "YEAR", "AGEGRP", "TOT_POP", "TOT_MALE", "TOT_FEMALE")

ageGroups <- ggplot(popSample, aes(y=TOT_POP)) + + 
  geom_boxplot(aes(x=AGEGRP), fill = "red", alpha = 0.5, color="darkred") +
  labs(title ="Populations", x = "Distribution", y = "Age Groups") +
  theme_light()
ageGroups
Jackson Walker
  • 137
  • 1
  • 9
  • 4
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Feb 09 '20 at 02:00

1 Answers1

0

I don't have access to your data but try:

ggplot(popSample, aes(x = AGEGRP, y=TOT_POP)) +  
  geom_boxplot(fill = "red", alpha = 0.5, color="darkred") +
  labs(title ="Populations", x = "Age Groups" y = "Distribution") +
  theme_light()

Let me know what else you need.

akash87
  • 3,876
  • 3
  • 14
  • 30
  • Thank you @akash87, here's the data: url <- "https://www2.census.gov/programs-surveys/popest/datasets/2010-2018/counties/asrh/cc-est2018-alldata.csv" if (!file.exists("./datafiles/cc-est2018-alldata-54.csv")) download.file(url, destfile = "./datafiles/cc-est2018-alldata.csv") – Jackson Walker Feb 09 '20 at 04:17
  • Thanks for that @akash87, when I run the code it says "Warning message: Continuous x aesthetic -- did you forget aes(group=...)? " – Jackson Walker Feb 09 '20 at 04:18
  • Since `AGEGRP` is already a numeric variable, you may want to use `as.integer` as opposed to `as.factor`. – akash87 Feb 09 '20 at 05:25