0

I have a dataset in R with thousands of geolocalized observations and I have to plot them on a map. I managed to plot single points using this code:

originale<-read.table("file.txt", header=TRUE,sep=";")
require(ggplot2)
require(ggmap)
map <- get_map(location = c(lon=13.781693, lat=45.623124), zoom = 14, maptype = "terrain",source = "google")
p <- ggmap(map)
p_punti <- p + geom_point(data=originale, aes(x=lon, y=lat),size=5)
plot(p_punti)

Now I'd like to plot them using a set of 5-6 colours based on the number of observation located on that point. Like this one that I've created on CartoDB: https://i.stack.imgur.com/Be1DL.png

The map plotted with CartoDB is, indeed, too approximate: I need to set my own range for each colour.

  • 1
    [Here's what I came up with](http://stackoverflow.com/a/17096661/980833) as an answer to a similar question. – Josh O'Brien Feb 25 '15 at 17:27
  • Your question doesn't really give enough information for an answer. If you already have a column with the counts you can just use `geom_point(data=originale, aes(x=lon, y=lat, color = counts))`. You can control the colors used with `scale_color_manual`. – Ista Feb 25 '15 at 18:05
  • I'm sorry @Ista, I'm trying to be more specific. The interesting part of the dataset is made like this: 'number longitude latitude; 1 45.654 13.645; 2 44.876 12.987; 3 45.654 13.645. As you can see several rows have the same coordinates so I don't have the counts. I thought not to have the counts in order to subset evry time the part of the dataframe that I want. But after your observations I'll try this way. – paolo.posocco Feb 25 '15 at 20:00
  • Great, please update the question with this additional information, including a sample of the data. – Ista Feb 25 '15 at 20:20

1 Answers1

0

I found a working solution counting recurrence for each observations in a new dataframe and then plotting the map using geom_point combined with scale_colour_discrete and breaks.

ThanksJosh and Ista for your suggestions!

Here's the code:

require(plyr)
df <-read.table("input.txt", header=TRUE,sep=";")

##selection
original <- df[(df$Year==2013),] #

##count recurrence 
conteggi2 <- ddply(original, .(original$lon, original$lat), nrow)
names(conteggi2) <- c("lon", "lat", "Freq")

##sort
conteggi2 <- conteggi2[with(conteggi2, order(Freq)), ]

##Classification
verdi <- conteggi2$Freq <= 5
conteggi2$classe[verdi] <- "1-5"
gialli <- conteggi2$Freq > 5 & conteggi2$Freq <= 20
conteggi2$classe[gialli] <- "6-20"
rossi <- conteggi2$Freq > 20 & conteggi2$Freq <= 50
conteggi2$classe[rossi] <- "21-50"
neri <- conteggi2$Freq > 50 & conteggi2$Freq <= 500
conteggi2$classe[neri] <- "51-500"

##plot map
require(ggplot2)
require(ggmap)
library(reshape2)
Trieste <- qmap(location = c(lon=13.781693, lat=45.623124), zoom = 13,
               legend = "topleft", color="bw")#maptype = "terrain",
Trieste1 <- Trieste +
        geom_point(aes(x = lon, y = lat, colour = classe, 
                       title='Year 2013'),
                        data = conteggi2) +
        guides(size=FALSE) + #remove useless label
        scale_colour_discrete(name="Legend Title",
                              breaks=c("1-5","6-20","21-50", "51-500"))
plot(Trieste1)