1

I did a Cluster Analysis and now I want to display the different groups on a map.

I did an example of the dataframe I have (the original one is way too big).

x1 <- c("Station1", "Station2", "Station3", "Station4", "Station5", "Station6", "Station7", "Station8")
x2 <- c(1, 2, 1, 3, 4, 1, 5, 3)
x3 <- seq(13.0, 15.3, length=8)
x4 <- seq(45.0, 48.0, length=8) 
x5 <- c("rural", "urban", "rural", "suburban", "urban", "suburban", "ubran", "rural")

TestCluster = data.frame(Station = x1, Cluster = x2, LON = x3, LAT = x4, Area = x5)

>TestCluster
  Station Cluster      LON      LAT     Area
1 Station1       1 13.00000 45.00000    rural
2 Station2       2 13.32857 45.42857    urban
3 Station3       1 13.65714 45.85714    rural
4 Station4       3 13.98571 46.28571 suburban
5 Station5       4 14.31429 46.71429    urban
6 Station6       1 14.64286 47.14286 suburban
7 Station7       5 14.97143 47.57143    ubran
8 Station8       3 15.30000 48.00000    rural

I want to display each Station on a map and use different colors for each Cluster.

I am using this code, but I always get 2 different error messages.

library(ggmap)
library(ggplot2)

Europe <- get_map(location = "Europe", zoom = 4)
p = ggmap(Europe)

p = p + geom_point(data = TestCluster, aes(LON, LAT, color = Cluster), size = 1) + 
    scale_color_manual(name = "Cluster", values = c("1" = "yellow",
                                                    "2" = "orange",
                                                    "3" = "red",
                                                    "4" = "green",
                                                    "5" = "blue"))
p

I get these error messages:

Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.

Error: Continuous value supplied to discrete scale

I read that the 2nd error should be solved with the color = as.factor(Cluster) command, but it doesnt work for me.

Any idea why it doesnt work?

jazzurro
  • 23,179
  • 35
  • 66
  • 76
Essi
  • 761
  • 3
  • 12
  • 22
  • 1
    Changing it to `color=as.factor(Cluster)` does remove the second error for me. I get the first error if I run the `p = p + geom_point(...` command twice. – Jason Jan 19 '18 at 19:55

1 Answers1

2

I just saw this question now. As Jason said, you want to treat Cluster as factor. This solves the second issue. As for the first warning message, I could not replicate it. I successfully produced a map. I changed the location when I downloaded a map given where your data points are. You may want to check which version of ggplot2 and ggmap you are using. This may be one of the first things to check. In my case, I used ggmap_2.6.1 and ggplot2_2.2.1 with R version 3.4.3.

library(ggplot2)
library(ggmap)

Europe <- get_map(location = "Munich", zoom = 6)

ggmap(Europe) +
geom_point(data = TestCluster, aes(x = LON, y = LAT, color = factor(Cluster)), size = 3) +
scale_color_manual(name = "Cluster", 
                   values = c(`1` = "yellow",
                              `2` = "orange",
                              `3` = "red",
                              `4` = "green",
                              `5` = "blue"))

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Thank you very much! I found now a solution by myself. Somehow there are problems because I use numbers instead of strings. When I rename "1" in "Cluster1" there are no problems anymore! – Essi Feb 04 '18 at 14:52
  • @Essi In your original code, the numbers are numeric, not character. So if you type "1", that would not work. If you wanna use numbers as numeric, you would need to use `` . I am glad that you found your way! – jazzurro Feb 04 '18 at 14:58
  • Hi, thanks! I tried the is.numeric, but it still made problems, I only had 1 one error less. :/ – Essi Feb 04 '18 at 23:49
  • @Essi On my machine, both versions give me the graphic above. – jazzurro Feb 05 '18 at 01:03
  • Thx a lot! You seem to be really good at these things. I just have another problem. Can I contact you maybe somehow via PM? I already asked here: https://stackoverflow.com/questions/48614328/r-ggplot-how-to-automatically-set-point-color-based-on-values?noredirect=1#comment84225916_48614328 – Essi Feb 05 '18 at 01:05
  • @Essi I just read the question now. Gregor has been giving you all tips you need. What questions do you have? If necessary I can help you via PM. – jazzurro Feb 05 '18 at 01:12
  • Yes please! I managed to create a map with the colour gradient, but everything is nearly in the same colour. The gradient only goes over one colour instead of 3. So you cant see differences at all. – Essi Feb 05 '18 at 01:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164514/discussion-between-jazzurro-and-essi). – jazzurro Feb 05 '18 at 01:18
  • Yes, thank you so much! Can you contact me? I dont know where to find it. – Essi Feb 05 '18 at 01:19