I am trying to find the centre coordinates of high-density areas in R.
The dataset I have has about 1.5million rows and looks like this (dummy data)
LATITUDE LONGITUDE val
1 35.83111 -90.64639 359.1
2 42.40630 -90.31810 74.5
3 40.07806 -83.07806 115.4
4 40.53210 -90.14730 112.0
5 42.76310 -84.76220 118.4
6 39.29750 -87.97460 134.4 ...
...
After plotting it using ggmap and ggplot using the command
ggmap(UK_Map) +
geom_density2d(data=processedSubsetData,aes(x=processedSubsetData$Longitude,y=processedSubsetData$Latitude), bins=5) +
stat_density2d(data=processedSubsetData,aes(x=processedSubsetData$Longitude,y=processedSubsetData$Latitude,fill=..level.., alpha=..level..), geom='polygon')
I have the visualization which looks like below image.
As you can see from the image, there some high-density areas. I need to find the local centre coordinates of these high-density areas in the map.
I have tried calculating distance between the points and also rounding the coordinates to group them. But I am not able to make it work and is stuck.
Thanks
Related
I'm considering a geopandas DataFrame with population data mapped to zip codes.
Its head looks like
geometry plz NUTS3 einwohner
0 POLYGON ((9.36585 54.69994, 9.36683 54.70014, ... 24988 DEF0C 3350
1 POLYGON ((12.47666 49.13598, 12.47702 49.13637... 93185 DE235 1786
2 POLYGON ((12.54904 49.19318, 12.54953 49.19371... 93489 DE235 2622
3 POLYGON ((12.62945 49.28007, 12.62949 49.28013... 93494 DE235 2018
4 POLYGON ((12.76492 49.27279, 12.76496 49.27288... 93473 DE235 1931
Plotting einwohner (population)
fig, ax = plt.subplots()
plz_shape_gdf.plot(
ax=ax,
column='einwohner',
categorical=False,
legend=True,
cmap='hot_r',
alpha=0.8
)
yields a nice heatmap
Next I uniformly distribute the popultion of a NUTS3 region onto the zip code areas via
plz_shape_gdf['einwohner_uniform'] = ''
for nuts in plz_shape_gdf['NUTS3']:
maske = (plz_shape_gdf['NUTS3'] == nuts)
plz_shape_gdf.loc[maske, 'einwohner_uniform'] = plz_shape_gdf.loc[maske, 'einwohner'].mean()
Plotting over the column einwohner_uniform yields the correct heatmap I guess, but the colorbar is somewhat messed up (see picture below, sorry for its lenghtiness).
What's causing the issue?
Is it possible to fix this behavior?
I have a file with 17294 particles (17294 lines) and for each particle I have θ,r in 2 columns. Here are the coordinates of the first 4 particles (radians,AU):
0.49270177616816235 2.5620755754079405
0.90238849710652080 2.5619357395318492
-0.40974974698351807 2.5078418996893919
1.2044083323780090 2.5701067236161976
I am trying to create a plot (via Gnuplot) with my data, the radius of the particles (distance from the Sun) is from 2.5 to 8 AU and when I plot I get only half of the disk:
I want to create the whole disk, I know that radius isn't negative, so I tried to:
set rrange[8:8]
but it didn't work.
How can I do this ?
I'm using the map_data('world2') data in ggplot2 with geom_map to plot some data, but I've run into a bit of an issue. I've divided up my regions into 3 longitude ranges (40E-140E, 140E-100W,100W-40E). As you can see, it doesn't fit neatly either into the (180W,180E) or (0,360) longitude ranges of the two world maps, so when I try to plot the data with a longitude axis that has a range encompassing the data, I get a blank portion of map on the right.
Is there a way to plot an extra slice of the map (i.e. 0->40E if using 'world2') adjacent to longitude 359? I tried manually adjusting the longitude values by doing the following:
m<-map_data('world2')
m$long<-ifelse((m$long>=0 & m$long<=40),m$long+360,m$long)
ggplot(data=longdata)+
coord_fixed(xlim=c(40,400),ylim=c(-90,90)) +
geom_map(data= m, map = m, aes(map_id=region))
However, the resulting map is an ungodly mess, likely because the polygons are now all screwed up. Any thoughts?
AHA!
I just needed to modify m to the following:
m<-map_data('world2',wrap=c(40,400))
I'm trying to overlay the coastlines and country borders onto the R map I've drawn.
I'm trying the simple worldHires function to do this but it's not scaling correctly to my map. I downloaded my data from https://www.esrl.noaa.gov/psd/data/gridded/data.UDel_AirT_Precip.html and the file name is precip.mon.total.v401
This is my code when I try to plot my map (excluding the first few lines of reading data and variables into R):
>image.plot(lon,lat,precip[,ncol(precip):1,8],
main="Precipitation (August, 2001)",
xlab=expression(paste("Longitude(",degree,"E)")),
ylab=expression(paste("Latitude(",degree,"N)")),
zlim=c(0,15),xlim = c(min(lon),max(lon)),
ylim = c(min(lat),max(lat)),cex=0.5)
> map('world2Hires', fill=FALSE, add=T)
The boundary layer from world2Hires is very small, scaling from -90 to 90 for Latitude and 0 to 360 for Longitude.
My data's range is Latitude -180 to 180 and Longitude 0 to 720. So how do I scale the world2Hires to match my data? Or is that not possible? See image in link
In your question, you seem to be mixing the latitude values (-90:90) degrees with the matrix indices. Since the map is of resolution 0.5 degrees, these run 0:360. The same for the longitudes.
So from your question, I am not sure whether the problem is that your vectors "lon" and "lat" are incorrect (they should probably be something like lon=seq(0,360,by=0.5); lat=seq(-90,90,by=0.5)). Obviously, the map() command expects the co-ordinates to be in degrees.
But the illustration on the link you provided, shows that the data is on longitude [0,360] while the data in worldHires is on [-180,180].
I think the simplest solution is to use "world2Hires", which is the same map but with longitudes shifted to [0,360], which should fit your map.
As a side remark, using worldHires is possibibly not the best choice of world map. The country borders in some parts of the world are outdated, and the high resolution is not so important at a global scale. Just using "world2" (the "2" again is for the shift to [0,360]) would probably look just as good on a global map.
I may add that in the very near future, the next version of "map" will make such shifting of longitudes much easier, whithout the need for an extra data set.
I would like to plot 3d spheres over a map in specific points.
Radious of the spheres measures my variable in one time.
I have x,y for east and north axis in UTM projection.
In z is the time and I have 2 observations in 4 positions of my map.
I want to present how changed my variable in the space and time.
The image I have in Shapefile or Geotiff format.
I saw another 3d plot similar to this one but not with maps.
Example 1
Example 2
My plot should be like this one: