Get Started

Change variable types

For displaying data in ggplot2 graphics, it is often helpful to convert all the variables to the right data type. Usually, categorical variables like country in this example should be converted to factors before plotting them. You can do so using as.factor(). In your data set, two columns are still of type "character" – use mutate() to turn them into factors.

This is a part of the course

“Communicating with Data in the Tidyverse”

View Course

Exercise instructions

  • Turn the variable year into a factor. Before turning it into a factor, turn it into a number using as.numeric(): call the as.numeric() function within the as.factor() function call.
  • Turn the variable country into a factor.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Turn year and country into a factor
ilo_data_corrected <- ilo_data %>%
  mutate(year = as.___(as.___(year)),
        country = ___)

# See the results
ilo_data_corrected
Edit and Run Code

This exercise is part of the course

Communicating with Data in the Tidyverse

BeginnerSkill Level
5.0+
4 reviews

Leverage the power of tidyverse tools to create publication-quality graphics and custom-styled reports that communicate your results.

In this chapter, you will have a first look at the data you're going to work with throughout this course: the relationship between weekly working hours and monetary compensation in European countries, according to the International Labour Organization (ILO). After that, you'll dive right in and discover a stunning correlation by employing an exploratory visualization. You will then apply a custom look to that graphic – you'll turn an ordinary plot into an aesthetically pleasing and unique data visualization.

Exercise 1: Introduction to the dataExercise 2: Join the two data sets togetherExercise 3: Change variable types
Exercise 4: Filtering and plotting the dataExercise 5: Filter the data for plottingExercise 6: Some summary statisticsExercise 7: A basic scatter plotExercise 8: Add labels to the plotExercise 9: Custom ggplot2 themesExercise 10: Apply a default themeExercise 11: Change the appearance of titlesExercise 12: Alter background color and add margins

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free