R ggmap countour plot: Convert ..level.. to events per squared area of distance

半腔热情 提交于 2019-12-09 22:27:03

问题


Using the example in ggmap: Spatial Visualization with ggplot2, by David Kahle and Hadley Wickham of a filled contour plot of violent crimes (Figure 9 in the linked article), I would like to know if instead of ..level.., how I could instead show average violent crimes per square (1000) kilometers as for me this would be much easier to interpret.

Here is an extract of the code from that article:

require(ggmap)
violent_crimes <- subset(crime, offense != "auto theft" & offense != "theft" & offense != "burglary")
violent_crimes$offense <- factor(violent_crimes$offense, levels = c("robbery", "aggravated assault", "rape", "murder"))
# restrict to downtown
violent_crimes <- subset(violent_crimes, -95.39681 <= lon & lon <= -95.34188 & 29.73631 <= lat & lat <= 29.78400)

houston <- get_map("houston", zoom = 14)
HoustonMap <- ggmap(houston, legend = "topleft")
HoustonMap +
  stat_density2d(
    aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
    size = 2, data = violent_crimes,
    geom = "polygon"
  )  + guides(alpha=FALSE)

N.B. I found the links provided by @JasonAizkalns here to posts 1 and 2 useful in understanding ..level..

来源:https://stackoverflow.com/questions/34939054/r-ggmap-countour-plot-convert-level-to-events-per-squared-area-of-distance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!