googleVis
googleVis copied to clipboard
How to map categorical variables in gvisGeoMap/gvisGeoChart
I am attempting to create a map in which each US state is colored according to a character type categorical variable. I am able to get the map to render, but all of the tooltips show a numeric value instead of the character string in my variable. Here's a reproducible example:
library(googleVis)
library(datasets)
category <- c("A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E")
hover <- c(1:50)
data <- data.frame(state.name,category,hover)
Map <- gvisGeoMap(data, "state.name", "category", "hover",
options=list(region="US",
displayMode="regions",
resolution="provinces",
width=600, height=400))
plot(Map)
The map assigns colors correctly for "category," but the tooltip shows category as a number, rather than a letter A - E. Ultimately, I would like to be able to assign discrete colors to each category, rather than using a gradient, but I'll take one step at a time with my questions. Any help is much appreciated.
I would recommend that you use gvisGeoChart
instead of gvisGeoMap
as it doesn't rely on Flash. The interface is very much the same.
If I understand you correctly then you would like to show the letters also in the tooltip. So, I am not sure why you assign numbers to the hover variable rather then the categories again. However, I believe that internally the category letter is converted into a number to calculate a colour and hence, a number is presented in the tooltip, instead of the character.
Would the following work for you?
library(googleVis)
library(datasets)
category <- c("A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E",
"A","B","C","D","E")
data <- data.frame(state.name,category,hover=category)
Map <- gvisGeoChart(data, locationvar="state.name", colorvar="category",hovervar= "hover",
options=list(region="US",
displayMode="regions",
resolution="provinces", displayMode="text",
width=600, height=400))
plot(Map)
Thanks for the reply. I've actually experimented with gvisGeoChart as well, and it's facing the same issue.
As for why I'm treating hover separately, the answer is a bit more complicated:
- The map is intended to show which vendor in a certain industry performs the best along a particular set of metrics --- I can get this in my data --- with each color corresponding to a vendor.
- My hovervar will be showing some statistic about the best vendor's operations in each state. Which statistic is shown will be dynamically determined by user input. So it necessarily needs to be distinct from colorvar.
Do you know of some way to convert the assigned number back into a character string for display?There are a couple of other issues that arise out of the way googleVis is handling my data: (1) The colorvar value still shows up in the tooltip, and it will definitely confuse my users to see numbers instead of vendor names. I'm cool with just removing that from the tooltip entirely if that's possible; (2) The legend shows a gradient rather than a discrete set of bins, which also leads to interpretability issues for the user.
Are gvisGeoChart/gvisGeoMap incapable of mapping categorical variables, do you know? (If so, I'm open to suggestions for other packages: choroplethr can map my data correctly, but doesn't support tooltips, and to the best of my knowledge rCharts doesn't yet have the ability to modify the content of tooltips, but I haven't yet asked for advice from the rCharts dev team).
P.S. On an unrelated topic: I'm also a mathematician who works on data visualization and the use of R in the insurance industry, so I'm pleased to come across your blog. :)
Have you checked the online documentation from Google: https://developers.google.com/chart/interactive/docs/gallery/geochart?hl=en?
The Google Chart Forum might also be able to help: https://groups.google.com/forum/#!forum/google-visualization-api
Unfortunately, I think I can't be of much more assistance here.
I had checked the documentation, but I hadn't tried the Google Chart Forum. I'll post there. Thanks for the help!