I am completing the Museums and Nature Centers project in the intermediate course for ggplot2 in R.
I am stuck on task 8, which is part of a 4-task sequence to create a barchart (tasks 7-10)
Task 7 mentioned the following:
Our data also contains information on each museum’s region, representing groups of states. Create a stacked bar plot using
museums_df showing the count of museums by region (
Region.Code..AAM. ), mapping
Is.Museum to the
fill aesthetic. Convert
Region.Code..AAM. to a factor (e.g.
factor(Region.Code..AAM.) ) so
ggplot2 plots its levels as discrete rather than continuous values. Call this plot
I wrote down the following code and got the plot that I was expecting:
museum_stacked <- ggplot(data=museums_df) + geom_bar( aes(x=factor(Region.Code..AAM.)), fill=Is.Museum, position = "stack") museum_stacked
I got the following image:
Everything so far is good. Task 8 mentions the following:
Our plot is hard to read – right now, we don’t know what the region numbers correspond to. Use
scale_x_discrete() to rename the numeric labels to text according to the following table.
Similarly, add a
scale_fill_discrete() layer to relabel the “TRUE” and “FALSE” labels in our legend to “Museum” and “Non-Museum”.
Based on the plot we created, which region has the most museums?
I add the following lines to my code:
museum_stacked <- ggplot(data=museums_df) + geom_bar( aes(x=factor(Region.Code..AAM.)), fill=Is.Museum, position = "stack") + scale_x_discrete( labels = c( "1" = "New England", "2" = "Mid-Atlantic", "3" = "Southeastern", "4" = "Midwest", "5" = "Mountain Plains", "6" = "Western")) + scale_fill_discrete( labels = c( "FALSE" = "Non-Museum", "TRUE" = "Museum")) museum_stacked
However, the plot stays the same. I have tried everything that makes sense to me, but I cannot figure out what the issue is. Unfortunately there is no code solution that I can refer to to see my mistakes. I cannot complete tasks 9 and 10 until I finish task 8, so I would like to know what part of my code is wrong.
I appreciate all and any help. Thank you so much!