Museums and Nature Centers - Intermediate ggplot2 - Help needed


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)

Link to project:
Link to my code:

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 museum_stacked .

I wrote down the following code and got the plot that I was expecting:

museum_stacked <- ggplot(data=museums_df) + 
    position = "stack")

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.

Code Region
1 New England
2 Mid-Atlantic
3 Southeastern
4 Midwest
5 Mountain Plains
6 Western

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) + 
    position = "stack") + 
    labels = c(
      "1" = "New England", 
      "2" = "Mid-Atlantic", 
      "3" = "Southeastern", 
      "4" = "Midwest", 
      "5" = "Mountain Plains", 
      "6" = "Western")) + 
    labels = c(
      "FALSE" = "Non-Museum", 
      "TRUE" = "Museum"))

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! :slight_smile:

Your code looks mostly okay, however it looks like you’re closing the aesthetic argument of geom_bar at the wrong place. fill is also an aesthetic for geom_bar, and so instead of

    position = "stack")

it should be

    position = "stack")

That seemed to make it work correctly on my tests as the actual scale arguments are fine.

This worked! Thank you so much :smiley:
I can’t believe such a small issue was causing this.

If fill is supposed to go in aes(), then why did my code work earlier? Also why would this prevent the scale arguments from working?

Sorry that I do not know this, I’m still learning! :slight_smile: I appreciate the help.

1 Like

No problem at all! It’s possible that you had it correct earlier and just accidentally moved a bracket when adding in the scale, I’m not too sure honestly! However the second part I can answer! The aes() wasn’t preventing it from working, what was happening was the error was stopping the program, and it was just displaying the previous output, as such it looked like nothing was changing because nothing was!

I see that you got it solved but I thought I’d share my code too as I spent some time stuck in this part too.

In my case, I thought that the numbers in the first labels expression call didn’t need to be in brackets as they were factors. I was wrong. Anyway, this is what worked for me:

museum_stacked<- ggplot(museums_df, aes(
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(


yeah dude
that helped me so much