Original link:tecdat.cn/?p=12430

Original source:Tuo End number according to the tribe public number


In this article, I will create a town-level heat map of motor vehicle collisions in New York City. Data from New York City. In particular, I’ll go from town level collisions to street level collisions. Below, I load the GGMAP package and data.

Library (ggmap) comm=regexpr(',',d_clean$LOCATION) # create year variable d_clean$year=substr(d_clean$DATE,7,10)Copy the code

 

I use the following three functions to process my data. The boro () function subset is used with street names in a given municipality. The accident_freq () function calculates the crash frequency for each street and then merges the numbers. Assign_col () gets a collision level data set for a particular town (created using the accident_freq () function) and assigns each street a color ranging from white to a specified color (green, red, etc.). The more collisions the streets will be darker.

man_col=assign_col(man_freq,'dodgerblue')
bronx_col=assign_col(bronx_freq,'darkred')
brook_col=assign_col(brook_freq,'violet')
si_col=assign_col(si_freq,'darkgreen')
q_col=assign_col(q_freq,'darkgoldenrod4')
Copy the code

Finally, I use ggMAP’s get_map () function to get the NYC style map and add the geom_PATH layer. There is only one geom_PATH () layer per city. Geom_path () uses straight lines or “paths” to connect all longitude and latitude points on the same street. All coordinates in the group are joined. Each line is then assigned a color determined by assign_col () using the col = argument.

ny_plot+ geom_path(data=man,size=1,aes(x=man$long, y=man$lat,group=man$ON.STREET.NAME),col=man_col[man_freq$freqPerc])+ geom_path(data=bronx,size=1,aes(x=bronx$long, y=bronx$lat,group=bronx$ON.STREET.NAME),col=bronx_col[bronx_freq$freqPerc])+ geom_path(data=brook,size=1,aes(x=brook$long, y=brook$lat,group=brook$ON.STREET.NAME),col=brook_col[brook_freq$freqPerc])+ geom_path(data=si,size=1,aes(x=si$long, y=si$lat,group=si$ON.STREET.NAME),col=si_col[si_freq$freqPerc])+ geom_path(data=q,size=1,aes(x=q$long, y=q$lat,group=q$ON.STREET.NAME),col=q_col[q_freq$freqPerc])+ ggtitle('Street-Level NYC Vehicle Accidents by Borough')+ xlab(" ") +ylab(" ")Copy the code