0

I have a data frame such that each row contains location data (latitude and longitude) and categorical integer numeric values ranging from 1-4.

Something like this, but with more meaningful lat/lon values:

id   | lat  | lon  | cluster
---- | ---- | ---- | -------
1    | 12   | -32  | 3
2    | 15   | -36  | 1
3    | 16   | -37  | 3
4    | 11   | -37  | 1

I have plotted the points on a map, but I want to be able to specify the colors of the points. Can I specify, for example, that points belonging to cluster 1 appear red, points belonging to cluster 2 appear orange, points belonging to cluster 3 appear green, etc.?

m<-get_map(location=c(lon=0, lat=0), zoom=1)
ggmap(m, extent = "normal") + geom_point(aes(x = lon, y = lat, color = cluster), data = df)

I assume there's something I need to add to the aes() function of ggmap, but I can't figure it out.

Thanks!

jstelman
  • 36
  • 1
  • 8
  • First, change `colour=cluster` to `colour=factor(cluster)`, so that it will be categorical, rather than continuous. Then add `+ scale_colour_manual(values=c("red","orange","green","blue"))`. The cluster values will be ordered from 1 to 4, so the colors will be assigned in that order. You can also explicitly specify which cluster value gets which color. For example, you could do this: `values=c("3"="red","2"="orange","4"="green","1"="blue")`. – eipi10 Dec 23 '16 at 05:49
  • @eipi10 Thanks! That works perfectly! Now, I'm new to this Stack Overflow, and I want to give you credit, but I don't see how to mark my question as answered? – jstelman Dec 23 '16 at 06:08
  • I didn't add it as an answer, because I'm pretty sure this question has been asked before. I'll mark it as a duplicate as soon as I find a previous answer to link to. – eipi10 Dec 23 '16 at 06:19

0 Answers0