Museums and nature centers project

I am stuck at the Museums and nature centers project at step 7. That’s how far I’ve gone this far:

museum_stacked <- museums_df %>%
mutate(Region.Code…AAM. = factor(Region.Code…AAM.)) %>%
ggplot(museums_df, aes(x=Region.Code…AAM., fill=Is.Museum)) +


I am not sure how to incorporate the factor() argument.

Thanks for the help :slight_smile:

Hi, can you link to the exercise?

May be able to help out.

yes, I’m sorry, I forgot. Here’s the link:

1 Like

It’s been a couple of days, so I’m sorry if this is too late to be helpful.

One thing I see here is that you’re changing museums_df in the pipe and storing it as museum_stacked, but then you’re feeding ggplot the original museums_df data frame. I haven’t piped things directly into ggplot, but I assume it works like other pipes, and you don’t need to specify the data frame you’re referencing. If you take the museums_df argument out of the ggplot call, I think you’ll pipe in the museums_stacked data frame you’ve just created.

I believe factor() is a function, not a separate argument. Try this:
ggplot(aes(x = factor(Region.Code…AAM., fill = Is.Museum))
**Note that I didn’t specify a data frame because of your piping. Without the pipes, you’d just have museums.stacked as the first argument.

That tells R, even though the values of Region.Code…AAM. are numbers, treat the numbers as names and not as actual numbers.

Hope that helps.


thank you very much for the help. My code now looks like this:

museum_stacked <- ggplot(museums_df, aes(x = factor(Region.Code…AAM.), fill = Is.Museum)) +
geom_bar(stat = “identity”)

But it still won’t work. Can you spot the mistake?

Ok, I don’t have a reason for why this is the case, but I have a note in my stacked-bar-plots section (I took handwritten notes for myself) that says, “Try geom_bar() without an argument if it doesn’t work.”

Want to give that a try? I don’t know why that would fix it, but I actually think I got hung up at this point as well, and I made that note because it fixed it. I got the impression from the lesson that if we chose geom_bar over geom_col, we needed that stat argument, but I took it out in my trial and error and it helped.

1 Like

yes, that did the trick. Thank you so much :slight_smile:

1 Like

Hi, I’m encountering the same issue. My code looks like this:

museum_stacked ← ggplot (museums_df, aes (x= factor(Region.Code…AAM.), fill= Is.Museum)) + geom_bar ()

I’ve tried the proposed tricks and it still doesn’t work. Can you spot the mistake?



This is what ended up working for me:

museum_stacked <- ggplot(museums_df, aes(x = factor(Region.Code..AAM.), fill = Is.Museum)) +

It has been some time though and I cannot really remember the logic behind it. Hope this helps

1 Like