Skip to content

Instantly share code, notes, and snippets.

@mestachs
Created June 11, 2015 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mestachs/de56b782de87d774260e to your computer and use it in GitHub Desktop.
Save mestachs/de56b782de87d774260e to your computer and use it in GitHub Desktop.
library(ggmap)
library(readr)
data <- read_csv("./input/train.csv")
locations = c(left = -122.5222,
bottom = 37.7073,
right = -122.3481,
top = 37.8381)
map <-get_map(location = locations, zoom = 12, source = "osm", color="bw")
data1 <- data[data$Category == c("ASSAULT", "NON-CRIMINAL", "WEAPON LAWS",
"OTHER OFFENSES", "WARRANTS", "LARCENY/THEFT"),]
p <- ggmap(map) + stat_density2d(aes(x = X, y = Y, fill = ..level.., alpha = ..level..),
bins = 15, geom = "polygon", data = data1) +
scale_fill_gradient(low = "green", high = "red", name = "Crime\nDensity\nLevels") +
scale_alpha(range = c(0.25, 0.75), guide = FALSE) +
facet_wrap(~Category, ncol =3) + ggtitle("San Francisco Crimes Heatmap by Category")
#Save
ggsave("Heatmap_Crimes.png", p, width=14, height=10, units="in")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment