Reputation: 2867
I have three columns of data
A Latitude Longitude
0.03336078 54.7000 -8.783333
0.12310763 54.21667 -10.033333
0.02180472 54.5000 -8.266667
0.74797928 52.63333 -9.483333
0.89240053 53.0833 -6.533333
0.35005815 52.1500 -7.166667
I wish to plot these on a map where each point is represented by a circle and each circle is coloured using a range of values, this range of values and colours is as follows:
yellow 0.004:0.277 green 0.277:0.549 pink 0.549:0.822 red 0.822:1.094
I can plot the map and the points but I can't get the legend to work out, so far I have managed the following
Ireland <- get_map("Ireland", zoom=7)
I <- ggmap(Ireland)
map <- I + geom_point(data = initialData, aes(x = Longitude, y = Latitude, fill = A)) + labs(x = "", y = "") + scale_color_manual(values = c("yellow", "green", "pink", "red") +........???? )
I'm guessing I need to create a range of values as per above and match them to the colours but I am struggling to figure out how to do this. Can anyone give me a steer?
Upvotes: 1
Views: 1949
Reputation: 2867
With the pointers from @aosmith above, I was able to hack together the following code which has answered my original question and helps me move onto the next aspect of my project.
library(ggplot2)
library(ggmap)
setwd("C:/Users/Bo Bo/Documents")
csv <- read.csv("Data Sampler.csv", sep = ",", colClasses = c("character","numeric","integer","character","character","character","numeric","numeric"))
csv$A1 <- cut(csv$A,breaks = c(0.004,0.277,0.549,0.822,1.094),right = FALSE)
mapIreland <- get_map(location = c(lon = mean(csv$Longitude),lat = mean(csv$Latitude)), zoom = 7, maptype = "roadmap", scale = 2)
ggmap(mapIreland) + geom_point(data = csv, aes(x = csv$Longitude, y = csv$Latitude, fill = csv$A1), size = 5, shape = 21) + labs(x = "",y = "") + scale_fill_manual(values = c("yellow","green","pink","red"),breaks = c(0.004,0.277,0.549,0.822,1.094))
My next issue relates to adding the corresponding legend to the map such that cut variable is displayed in the top right hand corner of the map. I believe the argument "name" can be specified in the scale_fill_manual function but for some reason it is not displaying. I could be something to do with the zoom of the map, I am looking into this issue now.
Upvotes: 1