I have a data frame (X) that looks like this:

code   cluster    name   longt      latit
101      1         A   -89.6171 35.24992
105      1         B   -90.0154 35.10510
106      2         C   -89.5213 34.93277
111      1         D   -86.8642 36.34807
113      3         E   -86.6125 36.19958
114      2         F   -90.4611 30.02196

I would like to plot the observations based on the cluster using plotGoogleMaps function. I have done the following:

X1 <- X
coordinates(X1) <- ~longt+latit # convert to SPDF
proj4string(X1) <-  CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
ic <- iconlabels(X1$cluster, height=10,colPalette=rainbow(4), icon=TRUE)
m <- plotGoogleMaps(X1,filename='myMap.htm', iconMarker=ic, 
    colPalette=rainbow(4),mapTypeId='ROADMAP',layerName = 'Clusters')

But I am facing two problems:

  1. when I use rainbow(3) as colors, I get only 2 colors in my legend (instead of 3). But when I use rainbow(4), I get 4 colors.

  2. The legend doesn't represent the clusters, i.e. I don't get a color for each cluster (like one color for #1, one color for #2, one color for #3,...)

  3. The Icons plotted on google map are only 2 colors (Green and Red). Clusters 1 and 2 are all red, and cluster 3 is green. I would like to obtain a distinct color for each cluster.


1 Answer 1


Looking at the examples provided with the ?iconlabels function, the author uses the column landuse from the meuse dataset. That column is from class factor.

So I tried to convert your column cluster from class integer to class factor and it worked.

X = read.table(text="code,cluster,name,longt,latit

X$cluster = as.factor(X$cluster)
X1 <- X

coordinates(X1) <- ~longt+latit # convert to SPDF
proj4string(X1) <-  CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

ic <- iconlabels(X1$cluster,height=10,icon=TRUE,colPalette=rainbow(3))
m <- plotGoogleMaps(X1,zcol="cluster",filename='myMap.htm',iconMarker=ic, 

enter image description here

  • Thank you very much @Andre Silva. That is exactly what I was missing.
    – H_A
    Nov 29, 2014 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.