Exercise

Step 6: Efficiently calculate record highs and lows

To also add the record lows to the plot, you could do the same things as in the previous exercise: create a data frame past_lows, join it with present, figure out the record lows, and add yet another layer, with a blueish color.

This is not really the ggplot2 way to do things. Instead of adding two layers, and manually assigning a color, you can do something else: you can map a variable denoting a record high or low onto the color aesthetic!

Here you'll combine the previous two exercises to identify the record highs and lows in one step, assign them to a new data frame called extremes, and use this to map a color aesthetic. This will make both your data munging and plotting code more efficient.

Instructions

100xp

The dplyr command that creates past_extremes is already included.

Finish the second dplyr command to create record_high_low:

Instead of simply filtering out record highs or lows, use mutate() to create a new variable record.

The value of record should be "#0000CD" if temp < past_low.

The value of record should be "#CD2626" if temp > past_high.

Otherwise, record should be "#00000000" (transparant!). Fill these values in the the two ifelse statements.

Add a geom_point() to the ggplot() command:

Use record_high_low as dataset, and make sure to map record onto the col aesthetic.

To get the proper colors we need to use a scale_color_identity() layer as well. This takes the colour value from the actual value in the data frame (see above).