绘制ggmap并将geom_tile添加到同一个绘图中

时间:2014-09-05 13:37:54

标签: r plot ggplot2 ggmap

目前我有一个文件,我想将其叠加到地图上。我的脚本如下:

library(ggmap)
library("ggplot2")

dataset = read.csv('stats.data')

names(dataset)=c('x','y','color')
dftotal = data.frame(x=dataset$x,y=dataset$y, col=dataset$color)

### Set a range
lat <- c(min(dataset$y),max(dataset$y))                
lon <- c(min(dataset$x),max(dataset$x)) 
bb <- c(0,1,2,3,4,5,6,7,8,9)

### Get a map
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 13, maptype = "satellite", source = "google")
ggmap(map)
pdf(paste("map.pdf", sep=""))

palette <- c("#000000","#000099")
palette <- c(palette, "#990099","#BB0099")
palette <- c(palette, "#EE0099","#FF00AA")
palette <- c(palette, "#FF00FF","#FF77FF")
palette <- c(palette, "#77AA77","#33FF33")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")
palette <- c(palette, "#00FF00","#00FF00")

ggplot(dftotal, aes(x=x, y=y, colour=factor(col), fill=factor(col))) +
geom_tile(aes(alpha=0.5)) +
scale_x_continuous(limits = lon, expand = c(0,0)) +
scale_y_continuous(limits = lat, expand = c(0,0)) +
scale_fill_manual(values=palette,breaks = bb, labels=c("0","1","2","3","4","5","6","7","8","9 or more")) +
scale_colour_manual(values=palette,breaks = bb) +
theme(axis.text.y = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(axis.ticks.x = element_blank()) +
guides(color=FALSE) +
guides(alpha=FALSE) +
guides(fill=FALSE)

如果我这样做

map + geom_tile(data=dftotal, aes(...))

我收到错误。我没有坚持使用geom_tile,我也可以使用别的东西。我想绘制文件中包含的数据(可以找到它here进行尝试),颜色应该反映最后一列。有什么想法吗?

EDIT 我为每个人添加了一些数据,而不是下载文件。

11.2976436018,44.4748465014,20.0
11.2976436018,44.4750510851,21.0
11.2976436018,44.4752556688,3.0
11.2976436018,44.4754602525,4.0
11.2976436018,44.4756648362,4.0
11.2976436018,44.4758694198,40.0
11.2976436018,44.4760740035,4.0
11.2976436018,44.4762785872,5.0
11.2976436018,44.4764831709,7.0
11.2976436018,44.4766877546,8.0
11.2976436018,44.4768923383,10.0
11.2976436018,44.477096922,11.0
11.2976436018,44.4773015057,10.0
11.2976436018,44.4775060893,10.0
11.2976436018,44.477710673,4.0
11.2976436018,44.4779152567,0.0
11.2976436018,44.4781198404,1.0
11.2976436018,44.4783244241,1.0
11.2976436018,44.4785290078,2.0
11.2976436018,44.4787335915,22.0
11.2976436018,44.4789381751,21.0
11.2976436018,44.4791427588,22.0
11.2976436018,44.4793473425,23.0
11.2976436018,44.4795519262,21.0
11.2976436018,44.4797565099,14.0
11.2976436018,44.4799610936,16.0
11.2976436018,44.4801656773,1.0
11.2976436018,44.480370261,1.0
11.2976436018,44.4805748447,0.0
11.2976436018,44.4807794283,6.0
11.2976436018,44.480984012,9.0
11.2976436018,44.4811885957,19.0
11.2976436018,44.4813931794,36.0
11.2976436018,44.4815977631,21.0
11.2976436018,44.4818023468,37.0
11.2976436018,44.4820069305,22.0
11.2976436018,44.4822115142,15.0
11.2976436018,44.4824160978,28.0
11.2976436018,44.4826206815,28.0
11.2976436018,44.4828252652,22.0
11.2976436018,44.4830298489,21.0
11.2976436018,44.4832344326,2.0
11.2976436018,44.4834390163,4.0
11.2976436018,44.4836436,4.0
11.2976436018,44.4838481837,40.0
11.2976436018,44.4840527673,1.0
11.2976436018,44.484257351,1.0
11.2976436018,44.4844619347,20.0
11.2976436018,44.4846665184,30.0
11.2976436018,44.4848711021,30.0

1 个答案:

答案 0 :(得分:2)

我认为切换到geom_point(我也改变了alpha和[IMO]使得更容易扭曲ggplot对象的位)可能就是你需要的全部内容:

gg <- ggmap(map) + geom_point(data=dftotal, mapping=aes(x=x, y=y, colour=factor(col), fill=factor(col)), alpha=0.125)
gg <- gg + scale_x_continuous(limits = lon, expand = c(0,0)) + scale_y_continuous(limits = lat, expand = c(0,0))
gg <- gg + scale_fill_manual(values=palette,breaks = bb, labels=c("0","1","2","3","4","5","6","7","8","9 or more"))
gg <- gg + scale_colour_manual(values=palette,breaks = bb)
gg <- gg + theme(axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank())
gg <- gg + guides(color=FALSE, alpha=FALSE, fill=FALSE)
gg

enter image description here