I need your help!

If you find any typos, errors, or places where the text may be improved, please let me know. The best ways to provide feedback are by GitHub or hypothes.is annotations.

Opening an issue or submitting a pull request on GitHub

Hypothesis Adding an annotation using hypothes.is. To add an annotation, select some text and then click the on the pop-up menu. To see the annotations of others, click the in the upper right-hand corner of the page.

28 Graphics for communication

28.2 Label

Exercise 28.2.2

The geom_smooth() is somewhat misleading because the hwy for large engines is skewed upwards due to the inclusion of lightweight sports cars with big engines. Use your modeling tools to fit and display a better model.

First, I’ll plot the relationship between fuel efficiency and engine size (displacement) using all cars. The plot shows a strong negative relationship.

However, if I disaggregate by car class, and plot the relationship between fuel efficiency and engine displacement within each class, I see a different relationship.

  1. For all car class except subcompact cars, there is no relationship or only a small negative relationship between fuel efficiency and engine size.

  2. For subcompact cars, there is a strong negative relationship between fuel efficiency and engine size. As the question noted, this is because the subcompact car class includes both small cheap cars, and sports cars with large engines.

Another way to model and visualize the relationship between fuel efficiency and engine displacement after accounting for car class is to regress fuel efficiency on car class, and plot the residuals of that regression against engine displacement. The residuals of the first regression are the variation in fuel efficiency not explained by engine displacement. The relationship between fuel efficiency and engine displacement is attenuated after accounting for car class.

Exercise 28.2.3

Take an exploratory graphic that you’ve created in the last month, and add informative titles to make it easier for others to understand.

By its very nature, this exercise is left to readers.

28.3 Annotations

Exercise 28.3.1

Use geom_text() with infinite positions to place text at the four corners of the plot.

I can use similar code as the example in the text. However, I need to use vjust and hjust in order for the text to appear in the plot, and these need to be different for each corner. But, geom_text() takes hjust and vjust as aesthetics, I can add them to the data and mappings, and use a single geom_text() call instead of four different geom_text() calls with four different data arguments, and four different values of hjust and vjust arguments.

Exercise 28.3.2

Read the documentation for annotate(). How can you use it to add a text label to a plot without having to create a tibble?

Exercise 28.3.4

What arguments to geom_label() control the appearance of the background box?

  • label.padding: padding around label
  • label.r: amount of rounding in the corners
  • label.size: size of label border

Exercise 28.3.5

What are the four arguments to arrow()? How do they work? Create a series of plots that demonstrate the most important options.

The four arguments are (from the help for arrow()):

  • angle : angle of arrow head
  • length : length of the arrow head
  • ends: ends of the line to draw arrow head
  • type: "open" or "close": whether the arrow head is a closed or open triangle

28.4 Scales

Exercise 28.4.1

Why doesn’t the following code override the default scale?

Exercise 28.4.3

Change the display of the presidential terms by:

  1. Combining the two variants shown above.
  2. Improving the display of the y axis.
  3. Labeling each term with the name of the president.
  4. Adding informative plot labels.
  5. Placing breaks every 4 years (this is trickier than it seems!).

Exercise 28.4.4

Use override.aes to make the legend on the following plot easier to see.

28.5 Zooming

No exercises

28.6 Themes

No exercises

28.7 Saving your plots

No exercises

28.8 Learning more

No exercises