我在R中有一个包含数千个地理定位观测数据的数据集,我必须在地图上绘制它们。我设法使用此代码绘制单点:
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)
现在,我想根据位于该点的观察次数,使用一组5-6种颜色绘制它们。就像我在CartoDB上创建的那样:http://i.stack.imgur.com/Be1DL.png
用CartoDB绘制的地图确实太近似了:我需要为每种颜色设置自己的范围。
答案 0 :(得分:0)
我找到了一个工作解决方案,计算新数据框中每个观察的重复,然后使用geom_point结合scale_colour_discrete和中断来绘制地图。
感谢Josh和Ista的建议!
以下是代码:
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)