Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Factors and how to create them
Working with factors
13. More on factors and bar plots
Modifying factor variables
Summary

Instruction

This is what the bar chart from the previous exercise looks like:

It's okay, but it could be better. We want to know right away which group has the most observations, and that's not easy with the current layout. Let's order the factor levels using the forcats function fct_infreq(). It sorts factor levels by how frequently they occur in the data set. We simply put:

fct_infreq(survey\$age)

in the aes() argument and put the column name inside fct_infreq(), like this:

ggplot(survey, aes(fct_infreq(age))) +
geom_bar() + coord_flip()


Exercise

Using ggplot(), geom_bar(), coord_flip(), and fct_infreq(), plot the number of people in each marital status category. Use the mrt_status column from survey.

Stuck? Here's a hint!

Type:

ggplot(survey, aes(fct_infreq(mrt_status))) +
geom_bar() + coord_flip()