Optimizing the plot for mobile devices
The x-axis title is already quite superfluous because you've added labels for both years. You'll now add country labels to the plot, so all of the axes can be removed.
In this exercise, you're going to encounter something that is probably new to you: New data sets can be given to single geometries like geom_text()
, so these geometries don't use the data set given to the initial ggplot()
call. In this exercise, you are going to need this because you only want to add one label to each arrow. If you were to use the original data set ilo_data
, two labels would be added because there are two observations for each country in the data set, one for 1996 and one for 2006.
This exercise is part of the course
Communicating with Data in the Tidyverse
Exercise instructions
- A new data set
median_working_hours
was created so there will only be one label per country. Have a look at the structure of it withstr()
. - Use
median_working_hours
as thedata
argument in a newgeom_text()
call, in order to add labels for each country.- Correctly specify the required aesthetics for
geom_text()
:x
andlabel
which should point to the right variable in themedian_working_hours
data set.
- Correctly specify the required aesthetics for
- Remove all the axes and the background grid by specifying the
element_blank()
function for all theaxis.
and thepanel.grid
arguments in the customtheme()
call. - After running the final code, resize the plot window on the right to simulate a mobile device screen in portrait mode (narrow and tall) – all labels will fit the plot viewport.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Compute temporary data set for optimal label placement
median_working_hours <- ilo_data %>%
group_by(country) %>%
summarize(median_working_hours_per_country = median(working_hours)) %>%
ungroup()
# Have a look at the structure of this data set
___
ilo_dot_plot +
# Add label for country
geom_text(data = ___,
aes(y = country,
x = ___,
label = ___),
vjust = 2,
family = "Bookman",
color = "gray25") +
# Remove axes and grids
theme(
axis.ticks = ___,
axis.title = ___,
axis.text = ___,
panel.grid = ___,
# Also, let's reduce the font size of the subtitle
plot.subtitle = element_text(size = 9)
)