lbedogni
lbedogni

Reputation: 8007

Plot a ggmap and add geom_tile to the same plot

currently I have a file that I want to plot overlayed to a map. My script is the following:

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)

If I do

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

I get an error. I'm not stuck with geom_tile, I can also use something else. I want to plot the data contained in the file (it can be found here for trying it), and the color should reflect the last column. Any idea?

EDIT I add some data for everyone, instead of downloading the file.

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

Upvotes: 1

Views: 1139

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78832

I think switching to geom_point (I also changed the alpha and [IMO] made it easier to twiddle bits of the ggplot object) may be all that you need:

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

Upvotes: 2

Related Questions