Exercise

Adding text

Now that we are not coloring according to BMI category, we have to add the group labels manually. Also, we neglected to label the x-axis properly!

Here we'll use the label aesthetic inside geom_text(). The actual labels will be the FILL and X columns in the DF data frame. Since we have axes on the left and bottom of our plot, we'll add information to the top and right inner edges of the plot. We could have also added margin text, but that is a more advanced topic. This will be a suitable solution for the moment.

Instructions

100xp

To position our labels correctly, we need to calculate the midpoint between each xmax and xmin value. To get this, calculate the half difference between each pair of xmax and xmin then add this value to xmin.

For this instruction, you don't have to write code yourself. For the y label positions, we only want to work with the values at the maximum xmax, i.e. at the very end. The code to calculate the ytext column is already available. Try to understand it.

Now that xtext and ytext are available, we can add the labels to our plot. In the two geom_text() functions, separate aesthetics are defined that control the x and y positioning of the labels.

For the age groups, set the x position with xtext. The y position is fixed since our y axis is always going to end at 1.

For the labeling of the y axis, the second geom_text() has three aesthetics since the position on the right will depend on the size of our dataset in the future. Fill in the ___ accordingly. x depends on the max() of xmax. You will have to use the predefind ytext column for y. The correct label is available in the FILL column.

Some additional attributes have been set inside geom_text(), outside the aes() function. This is just some fine tweaking to get the positioning and angle correct. It's not perfect, but since this is an exploratory plot, it does a pretty good job.