1

I am working on making a plotly map of the US with hover tooltips which I have gotten to work somewhat. However, I have multiple observations per state for each variable I would like to display in the tooltip and currently only the first observation for each state is displayed. Each observation is the performance of a candidate in the 1976 presidential election in a state, and I would like the hover tooltip to display each candidates performance in the state instead of just the first candidate listed in that state.

Here is the code I am using at the moment.

candidate denotes the name of the candidate , state_share and round_share denote the percent of the state popular vote and state electoral votes the candidate receives respectively.

library(plotly)
colorscale <- c("blue" , "purple" , "red")

l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = FALSE
)

threshold10$hover <- with(threshold10, paste(state, "<br>" , canvec ,':', 
                                             state_share ,",","Electoral Votes", round_share ))
fig <- plot_geo(subset(threshold10, year == 1976), locationmode = 'USA-states') 

fig <- fig %>% 
  add_trace(
    z = ~evotes, text = ~ hover  , locations = ~state_po,
  color = ~state_share , colors = colorscale) %>% 
  layout(title = '1976 Electoral Vote Allocation <br> 10% State Threshold',
    geo = g)
fig

I'm also attaching an image of the dataset and the map produced by my code. I appreciate any help anyone has to offer. I am newish to working with plotly and mapping so if this is a simple question sorry about that. Thank you for your help.

dataframe: enter image description here

map output: enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
JonahKing
  • 11
  • 1
  • Welcome to Stack Overflow! You should provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It should be [minimal, but complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). Your question should be clear and specific. – M-- May 29 '20 at 00:42

0 Answers0