Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Factors
Summary

Instruction

Good job! The bars in the barplot are displayed in the same order as the levels in the price_category factor, that is "high", "low", "medium". This is their alphabetical order. However, it makes more sense to order the levels as high-medium-low or low-medium-high.

To define the order of factor levels, you can use the optional levels argument. For example, if you would like to change the order to "high", "medium", "low", you can do the following:

houses$price_category<- factor(houses$price_category, levels = c("high","medium","low"))

The levels argument allows you to specify the levels your factor will use and in what order they should be defined. In this case, c("high", "medium", "low") tells R to create a factor whose levels are defined as "high", "medium", and "low", in that specific order.

Exercise

Define the order of levels of the price_category factor as follows: "low", "medium", "high". Create a bar chart using the price_category variable. Observe that bars are now in the same order.

Stuck? Here's a hint!

Use barplot() in combination with the table() function. First, make a frequency table with the table() function, and then use it to construct the barplot.