我找到了一个可行的解决方案,计算新数据框中每个观察值的重复次数,然后使用 geom_point 结合 scale_colour_discrete 和中断绘制地图。
感谢乔希和伊斯塔的建议!
这是代码:
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)