R:用颜色强度在地图上绘制点
R: draw points on map with colour intensity
我在 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 绘制的地图确实太近似了:我需要为每种颜色设置自己的范围。
我找到了一个工作解决方案,计算新数据框中每个观测值的重复率,然后使用 geom_point 结合 scale_colour_discrete 和 breaks 绘制地图。
感谢 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)
我在 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 绘制的地图确实太近似了:我需要为每种颜色设置自己的范围。
我找到了一个工作解决方案,计算新数据框中每个观测值的重复率,然后使用 geom_point 结合 scale_colour_discrete 和 breaks 绘制地图。
感谢 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)