Get startedGet started for free

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.

This exercise is part of the course

Interactive Data Visualization with plotly in R

View Course

Exercise 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).

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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 = ___(___, ___))
Edit and Run Code