Hi I have been stuck on step 10 for almost a month now and cannot figure out what on earth is wrong with my code. Specifically, steps 7-10 are giving me the most difficulty and there is no walkthrough and cannot find the answer in the codecademy forums. I am not sure if there is a bug within the problem but I have even reset it and cannot progress past 7-10 because the stacked bar plot does not update.
This is the intermediate R course using ggplot2
Steps 7-10 are:
-
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.
), mappingIs.Museum
to thefill
aesthetic. ConvertRegion.Code..AAM.
to a factor (e.g.factor(Region.Code..AAM.)
) soggplot2
plots its levels as discrete rather than continuous values. Call this plotmuseum_stacked
. -
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 ascale_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?
- Rather than seeing counts, perhaps we’re more interested in the percentage of museums vs non-museums by region. Transform the plot we just created to a stacked bar plot showing values out of 100% by passing
position = "fill"
to ourgeom_bar()
layer. Apply thescales::percent_format()
function to transform oury
axis labels into percentage values.
How does the distribution of museum types vary by region?
- Our graph looks pretty good! However, our axes titles are a little non-descript. Using the
labs()
layer, let’s title this plot “Museum Types by Region”, relabel thex
axis title as “Region”, relabel they
axis title as “Percentage of Total”, and relabel thefill
legend title as “Type”.
Now, someone can take a look at this plot and immediately understand what is being described. There were a lot of steps here, but now our plot is clear and professional. Give yourself a pat on the back, and feel free to take a 10 minute coffee break before the next section!
This is the code I have on step 10:
museum_stacked <- ggplot(museums_df,
aes(x=factor(Region.Code..AAM.),
fill=Is.Museum),
position = "fill") +
geom_bar(
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"))
scale_y_continuous(
scales::percent_format()) +
labs(title = "Museum Types by Region", x = "Region", y = "Percentage of Total", fill = "Type"))
museum_stacked
this is the output graph: