Summing latitude, longitude, and count data for using ggplot

I was provided with some customer data in Latitude, Longitude, and Counts formats. All the data that I need to create a ggplot heatmap is present, but I don’t know how to insert it into the format required by ggplot.

I try to combine the data on the general calculations within the limits of 0.01 lats and 0.01 lon-blocks (a typical heat map), and I instinctively thought that this was a “branch”. This creates a good summary of the block size as desired, but the format is incorrect. In addition, I would really like the empty values ​​of the Lat or Lon blocks to be included in zero, even if there is nothing there ... otherwise the heat map ends up looking striped and odd.

Your help is greatly appreciated.

I created a subset of my data for your link in the code below:

# m is the matrix of data provided m = matrix(c(44.9591051,44.984884,44.984884,44.9811399, 44.9969096,44.990894,44.9797023,44.983334, -93.3120017,-93.297668,-93.297668,-93.2993524, -93.2924484,-93.282462,-93.2738911,-93.26667, 69,147,137,22,68,198,35,138), nrow=8, ncol=3) colnames(m) <- c("Lat", "Lon", "Count") m <- as.data.frame(m) s = as.data.frame((tapply(m$Count, list(round(m$Lon,2), round(m$Lat,2)), sum))) s[is.na(s)] <- 0 # Data frame "s" has all the data, but not exactly in the format desired... # First, it has a column for each latitude, instead of one column for Lon # and one for Lat, and second, it needs to have 0 as the entry data for # Lat / Lon pairs that have no other data. As it is, there are only zeroes # when one of the other entries has a Lat or Lon that matches... if there # are no entries for a particular Lat or Lon value, then nothing at all is # reported. desired.format = matrix(c(44.96,44.96,44.96,44.96,44.96, 44.97,44.97,44.97,44.97,44.97,44.98,44.98,44.98, 44.98,44.98,44.99,44.99,44.99,44.99,44.99,45,45, 45,45,45,-93.31,-93.3,-93.29,-93.28,-93.27,-93.31, -93.3,-93.29,-93.28,-93.27,-93.31,-93.3,-93.29, -93.28,-93.27,-93.31,-93.3,-93.29,-93.28,-93.27, -93.31,-93.3,-93.29,-93.28,-93.27,69,0,0,0,0,0,0, 0,0,0,0,306,0,0,173,0,0,0,198,0,0,0,68,0,0), nrow=25, ncol=3) colnames(desired.format) <- c("Lat", "Lon", "Count") desired.format <- as.data.frame(desired.format) minneapolis = get_map(location = "minneapolis, mn", zoom = 12) ggmap(minneapolis) + geom_tile(data = desired.format, aes(x = Lon, y = Lat, alpha = Count), fill="red") 
+2
source share
1 answer

Here is a hit with geom_hex and stat_density2d. The idea of ​​creating baskets by trimming the coordinates makes me a little complicated.

You have the count data with lat / longs, which means that ideally you need a weight parameter, but as far as I know, this is not implemented using geom_hex. Instead, we crack it by repeating the lines in the count variable, similar to the approach here .

  ## hack job to repeat records to full count m<-as.data.frame(m) m_long <- with(m, m[rep(1:nrow(m), Count),]) ## stat_density2d ggplot(m_long, aes(Lat, Lon)) + stat_density2d(aes(alpha=..level.., fill=..level..), size=2, bins=10, geom=c("polygon","contour")) + scale_fill_gradient(low = "blue", high = "red") + geom_density2d(colour="black", bins=10) + geom_point(data = m_long) ## geom_hex alternative bins=6 ggplot(m_long, aes(Lat, Lon)) + geom_hex(bins=bins)+ coord_equal(ratio = 1/1)+ scale_fill_gradient(low = "blue", high = "red") + geom_point(data = m_long,position = "jitter")+ stat_binhex(aes(label=..count..,size=..count..*.5), size=3.5,geom="text", bins=bins, colour="white") 

They accordingly produce the following: enter image description here And the empty version: enter image description here

EDIT:

With database:

 map + stat_density2d(data = m_long, aes(x = Lon, y = Lat, alpha=..level.., fill=..level..), size=2, bins=10, geom=c("polygon","contour"), inherit.aes=FALSE) + scale_fill_gradient(low = "blue", high = "red") + geom_density2d(data = m_long, aes(x = Lon, y=Lat), colour="black", bins=10,inherit.aes=FALSE) + geom_point(data = m_long, aes(x = Lon, y=Lat),inherit.aes=FALSE) ## and the hexbin map... map + #ggplot(m_long, aes(Lat, Lon)) + geom_hex(bins=bins,data = m_long, aes(x = Lon, y = Lat),alpha=.5, inherit.aes=FALSE) + geom_point(data = m_long, aes(x = Lon, y=Lat), inherit.aes=FALSE,position = "jitter")+ scale_fill_gradient(low = "blue", high = "red") 

enter image description hereenter image description here

+3
source

Source: https://habr.com/ru/post/1233524/


All Articles