1

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?

TheGoat
  • 2,587
  • 3
  • 25
  • 58
  • 1
    If you want to turn your continuous fill into categorical fill, `cut` the variable and then map the new factor as your fill variable and set colors via `scale_fill_manual` as shown in answers [here](http://stackoverflow.com/questions/10981324/ggplot2-heatmap-with-colors-for-ranged-values). Make sure you are using a fillable point for this, otherwise switch to `color`. – aosmith Oct 18 '16 at 23:23
  • 1
    If instead you want a continuous colorbar but using your colors and breaks, you may end up using `scale_fill_gradientn` where you set the colors and breaks much as in [this question](http://stackoverflow.com/questions/13888222/ggplot-scale-color-gradient-to-range-outside-of-data-range) – aosmith Oct 18 '16 at 23:28
  • @aosmith Thanks so much for the pointers, even with your direction I struggled for a while but I finally got there. Thanks again. – TheGoat Oct 19 '16 at 21:25
  • Glad to hear it. You can put your final solution as an answer. – aosmith Oct 19 '16 at 21:38

1 Answers1

1

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.

TheGoat
  • 2,587
  • 3
  • 25
  • 58