CommencerCommencer gratuitement

Putting a rug down

The plot we just made looks pretty good, but we can see a small hint of bi-modality between the hours of 10 and 15. Given we just are looking the blob of a density estimate it's hard to know how confident we are in that shape.

Let's alleviate this by doing a couple things.

First, lower the alpha on the density plot so we can more accurately locate these peaks using the background grid.

Next, add a rug-plot beneath the density plot using geom_rug(). To show any overlap that may occur in the rug plot set its alpha to 0.5.

The data frame you made in the last exercise: truck_speeding has already been loaded for you.

Cet exercice fait partie du cours

Visualization Best Practices in R

Afficher le cours

Instructions

  • Set alpha of density plot to 0.7.
  • Add geom_rug() to the plot.
  • Set alpha of rug plot to 0.5.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

ggplot(truck_speeding, aes(x = hour_of_day)) +
    # Adjust opacity to see gridlines with alpha = 0.7
    geom_density(bw = 1.5, fill = 'steelblue', ___) +
    # add a rug plot using geom_rug to see individual data points, set alpha to 0.5.
    ___ +
    labs(title = 'Citations by hour', subtitle = "Gaussian kernel SD = 1.5")
Modifier et exécuter le code