CommencerCommencer gratuitement

Which candidate spent the most?

As you saw, most Senate campaigns raised under $1M and the vast majority raised under $20M, so what races raised these astronomical amounts? Histograms bin observations, obscuring easy identification of individual candidates, so a different chart is needed to explore this question.

Your task is to create a dotplot of the 15 Senate campaigns that raised the most money during the 2018 election cycle. You will also need to customize the hover info to facilitate easy identification of the candidates.

Focus first on creating the plot, but be sure to review how the hover info was customized!

Note that plotly has already been loaded for you.

Cet exercice fait partie du cours

Interactive Data Visualization with plotly in R

Afficher le cours

Instructions

  • For the top 15 campaigns, create a dotplot (i.e. scatterplot) displaying receipts on the x-axis and state on the y-axis, where state has been reordered by receipts.
  • Change the colors so that blue represents Democrats (DEM) and red represents Republicans (REP).

Exercice interactif pratique

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

# Create a dotplot of the top 15 Senate campaigns
fundraising %>%
  filter(office == "S") %>%
  slice_max(receipts, n = 15) %>%
  plot_ly(x = ___, y = ~fct_reorder(___, ___),
          color = ~fct_drop(party),
          hoverinfo = "text",
          text = ~paste("Candidate:", name, "
", "Party:", party, "
", "Receipts:", receipts, "
", "Disbursements:", disbursement)) %>% add_markers(colors = ___(___, ___))
Modifier et exécuter le code