Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot the intensity of a continuous with geom_tile in ggplot

I'm trying to plot a continuous variable on space. I saw this example that gets the same result that I need:

library("MASS")
library("ggplot2")
library(reshape2) 

DB<-melt(volcano)
ggplot(DB, aes(x=Var1, y=Var2, fill=value)) +geom_tile()

enter image description here

So I did with my data:

library(repmis)
url<-"https://www.dropbox.com/s/4m5qk32wjgrjq40/dato.RDATA"
source_data(url)

library(ggplot2)
ggplot(dato,aes(y=variable,x=y,fill=value))+geom_tile()

enter image description here

That's wonderful. But my "x" and "y" are kilometers distance (east and north) from a point in space. I transformed these in latitude and longitude. But now my plot doesn't work!

ggplot(dato,aes(y=lat,x=long,fill=value))+geom_tile()

enter image description here

I don't understand why. Anyway plotting my data like points the result is very similar:

ggplot(dato,aes(y=lat,x=long,fill=value))+geom_point()
ggplot(dato,aes(y=variable,x=y,fill=value))+geom_point()
like image 248
dax90 Avatar asked Dec 08 '22 02:12

dax90


1 Answers

You can cheat a bit and use geom_point with a square shape:

#devtools::install_github("sjmgarnier/viridis")
library(viridis)
library(ggplot2)
library(ggthemes)
library(scales)
library(grid)

gg <- ggplot(dato)
gg <- gg + geom_point(aes(x=long, y=lat, color=value), shape=15, size=5)
gg <- gg + coord_equal()
gg <- gg + scale_color_viridis(na.value="#FFFFFF00")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg

enter image description here

I did not project the lat/long pairs and just used coord_equal. You should use a proper projection for the region being mapped.

And, now you have me curious as to what those hot spots are around Milan :-)

gmap <- get_map(location=c(9.051062, 45.38804, 9.277473, 45.53438),
                 source="stamen", maptype="toner", crop=TRUE)
gg <- ggmap(gmap)
gg <- gg + geom_point(data=dato, aes(x=long, y=lat, color=value), shape=15, size=5, alpha=0.25)
gg <- gg + coord_map()
gg <- gg + scale_color_viridis(na.value="#FFFFFF00")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="right")
gg

enter image description here

like image 110
hrbrmstr Avatar answered Mar 08 '23 03:03

hrbrmstr