wsda
wsda

Reputation: 1405

Tooltip in ggvis for R with layer_paths

I have a trouble about the tooltip function in ggvis for my choropleth plot.I have attached my code below. The code is running good with no tooltip function. But if I add tooltip, only the land of new jersey left(see pic). The other states are missing. Any one can help to have a look and how can I fix it to get the other parts back with tooltip function added? Thanks a lot! pic

###my data for example
mapdata<-data.frame(
  state=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
  revenue=runif(50,min=100,max=9000))

library(rgdal)   
library(ggplot2) 
library(ggvis)

url   <- "http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip"
tf    <- tempfile()
td    <- tempdir()
download.file(url,tf, mode="wb")  
unzip(tf, exdir=td)                

usa <- readOGR(dsn=td, layer="cb_2014_us_state_20m")
shp <- usa[(!usa$STUSPS %in% c("AK","HI")),] 

df<- fortify(shp)                    
df<- merge(df,cbind(id=rownames(shp@data),shp@data),by="id")   
df$state <- tolower(df$NAME)                                                  
df<- merge(df,mapdata,by="state")  
df<- df[order(df$order),]       

####function for tooltip
gg<-df
gg$id<-1:nrow(gg)
values<-function(x){
  if(is.null(x)) return(NULL)
  row<-gg[gg$id==x$id,]
  paste0("State: ",row$state,"<br />",
         "Revenue: ", row$revenue, "<br />")
}
####ggvis choropleth
gg %>%
  group_by(group) %>%
  ggvis(~long, ~lat, key:=~id)  %>%
  hide_axis("x") %>% 
  hide_axis("y")%>%
  add_tooltip(values,"hover")%>%
  layer_paths(fill= ~revenue)

Upvotes: 3

Views: 1342

Answers (1)

aosmith
aosmith

Reputation: 36076

I was looking around a bit and realized that since paths are made from information from multiple rows of a dataset, it doesn't make sense to try to use a unique row id for the tooltip like we usually do with key.

Then I saw this very nice ggvis tutorial, and the way forward became clearer to me. You need to base the tooltip on your grouping variable, and then somehow winnow the info from the many rows in that group down to a single value. Among other options, you could do this by pulling out just the first row from the group or by using just the unique value when making your in your tooltip function.

Here's examples of what I mean, using the variable group that you use in group_by to pull out the pertinent info.

First, using head to get just the first row:

values = function(x){
    if(is.null(x)) return(NULL)
    row = head(gg[gg$group == unique(x$group), ], 1)
    paste0("State: ", row$state,"<br />",
           "Revenue: ", row$revenue, "<br />")
}

Alternatively, just pull the unique values of the variables you want to display (there is one per state/group). This works OK in your case because you are only displaying two values:

values2 = function(x){
    if(is.null(x)) return(NULL)
    row = gg[gg$group == unique(x$group), ]
    paste0("State: ", unique(row$state),"<br />",
           "Revenue: ", unique(row$revenue), "<br />")
}

Now take the key argument out of ggvis and use one of the new tooltip functions. Now the interactive graph appears to work fine.

gg %>%
    group_by(group) %>%
    ggvis(~long, ~lat)  %>%
    hide_axis("x") %>% 
    hide_axis("y")%>%
    add_tooltip(values, "hover")%>%
    layer_paths(fill= ~revenue)

Upvotes: 4

Related Questions