I am using the method outlined in this post in order to create a density heatmap on a map paired with the one dimensional x and y density plots. I was able to produce the following image: Density Heatmap
However, I am having difficulty aligning the shaded regions in the top density plot with the heatmap on the map. I discovered that this has to to with the fact that I am applying a mercator projection in the code to produce the heatmap, whereas the top and right density plots are just using the coordinates from the data.
Is there a way that I can align the projected coordinates in the heatmap with the nonprojected coordinates in the one dimensional density plot?
Below is the relevant section of my code (I used the grid/gtable packages to combine the different graphs).
EDIT: The data is comprised of three columns: UniqueID, Lon, and Lat. In this case all the Lon/Lat pairs are in/around New York City, but for troubleshooting purposes the exact pairs don't matter as long as they can be projected.
library(gtable)
library(ggplot2)
library(ggmap)
ny <- get_map(location = "Washington Heights, New York City", zoom = 12, color = "bw")
ggm <- ggmap(ny, extent = "normal", maprange = F) %+% data +
aes(x = Lon, y = Lat) +
stat_density2d(aes(fill = ..level..,
alpha = ..level..), color = "darkgreen",
geom = "polygon", show.legend = TRUE) +
geom_polygon(aes(x, y),
data.frame(x = c(Inf, Inf, -73.90, -73.90), y = c(Inf, 40.84, 40.84, Inf)),
alpha = 0.5, colour = NA, fill = "red") +
coord_map(projection = "mercator",
xlim = c(attr(ny, "bb")$ll.lon, attr(ny, "bb")$ur.lon),
ylim = c(attr(ny, "bb")$ll.lat, attr(ny, "bb")$ur.lat))
xd <- data.frame(density(data$Lon)[c("x", "y")])
gg1 <- ggplot(xd, aes(x, y)) +
theme(panel.grid.minor = element_line(colour = NA),
panel.background = element_rect(fill = NA, colour = NA)) +
labs(y = "Density") +
geom_area(data = subset(xd, x > -73.90), fill = "red") +
geom_line() +
coord_cartesian(c(attr(ny, "bb")$ll.lon, attr(ny, "bb")$ur.lon))
image <- gtable_filter(ggplotGrob(ggm), pattern = "panel", trim = TRUE, fixed=TRUE)
image <- gtable_add_cols(image, unit(0.2, "null"), 1)
image <- gtable_add_grob(image, gtable_filter(ggplotGrob(gg1), pattern = "panel", trim = TRUE, fixed=TRUE), 1, 1)