Set color of point depending on a value

I have a dataframe that looks like this:

x1= c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6")
x2= c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30)
x3= c(16.55, -2.10, 8.80, 23.70, 24.50, 14.40)
x4= c(342, 1900, 283, 832, 212, 1533)
x5= c("rual", "rual", "urban", "suburban", "rual", "urban")

testframe = data.frame(Station=x1, LAT=x2, LON=x3, ALT=x4, AREA=x5) 

I want to display the points in 3 different colors. Green for rual, yellow for suburban, red for urban.

But until now I only managed to display them all in one color. I didn this:

library(ggmap)
library(ggplot2)

Europe = get_map(location = "Europe", zoom = 4)

p = ggmap(Europe)
p = p + geom_point(data=testframe, aes(x=testframe$LON, y=testframe$LAT), color = "red", size=1)
p

Can someone help me out please?

1 answer

  • answered 2018-01-11 21:34 tyluRp

    You could try the following:

    p + 
      geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) +
      scale_color_manual(name = "AREA", values = c("rual" = "darkgreen",
                                                   "suburban" = "yellow",
                                                   "urban" = "red"))
    

    enter image description here

    Or copy/paste this chunk of code:

    library(ggmap)
    library(ggplot2)
    
    x1 = c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6")
    x2 = c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30)
    x3 = c(16.55, -2.10, 8.80, 23.70, 24.50, 14.40)
    x4 = c(342, 1900, 283, 832, 212, 1533)
    x5 = c("rual", "rual", "urban", "suburban", "rual", "urban")
    
    testframe = data.frame(Station = x1, 
                           LAT = x2, 
                           LON = x3, 
                           ALT = x4, 
                           AREA = x5) 
    
    Europe = get_map(location = "Europe", zoom = 4)
    
    p = ggmap(Europe)
    
    p + 
      geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) +
      scale_color_manual(name = "AREA", values = c("rual" = "darkgreen",
                                                   "suburban" = "yellow",
                                                   "urban" = "red"))