问题描述
我有洛杉矶港口地区的颗粒物浓度差异(之后-之前).我正在尝试使用ggmap在地图上绘制浓度等高线,但结果看起来却大不相同.我使用的代码如下所示(数据在代码下方):
I have Particulate Matter concentration difference (After - Before) for Port of Los Angeles area. I am trying to use ggmap to plot concentration contours on map but the result looks way different. The code I used is shown below (and data is below the code):
library(ggmap)
PM = read.csv('data.csv', stringsAsFactors = FALSE)
获取经度和纬度
geocode("Port of Los Angeles") # Not centered
geocode("Compton, CA") # Now centered
使用Compton LON和LAT
POLA = c(lon = -118.220071, lat = 33.895849)
POLA.map = get_map(location = POLA, zoom = 10, color = 'bw')
ggmap(POLA.map) + geom_point(data = PM, mapping = aes(Longitude, Latitude)) +
stat_density2d(data = PM, mapping = aes(x = Longitude, y = Latitude, fill=..level..), geom = "polygon", alpha = 0.3, contour = TRUE)
但是,等高线图应具有以下模式:
https://drive.google.com/file/d/0B3XVjcsci0y3VDBTc01PYkhOckE/view?usp = sharing
ggplot(PM, aes(UTM.X, UTM.Y)) + geom_tile(aes(fill = Value), alpha = 0.8, color = "black") +
scale_fill_gradient(low = 'green', high = 'red')
数据:第1列:经度,第2列:纬度,第3列:UTM-X,第4列:UTM-Y,第5列:值
UTM坐标单位:米,UTM区域= 11 N,基准= WGS84.可在此处获取数据: https://drive.google.com/file/d/0B3XVjcsci0y3LUpudko1S2c1cnc/view?usp = sharing
推荐答案
stat_density2d
用于绘制密度图,例如深色的点很多,浅色的点很多.您有一个具有 Value
属性的常规网格,而不是密度图.
stat_density2d
is used for plotting density maps, for example dark colours where there are lots of points and light colours where there's few. You have a regular grid with a Value
attribute, not a density plot.
因此,您应该使用 geom_tile
来获取常规的网格图.但是您的经纬度坐标不会形成轴对齐的网格.试试这个:
So you should be using geom_tile
to get a regular grid map. But your lat-long coordinates do not form an axis-aligned grid. Try this:
ggplot(data = PM, mapping = aes(x = Longitude, y = Latitude, fill=Value)) + geom_tile()
您将得到一个空白图,请尝试以下操作:
and you get a blank plot, try this:
ggplot(data = PM, mapping = aes(x = UTM.X, y = UTM.Y, fill=Value)) + geom_tile()
,您将获得情节.当然,它与 ggmap
背景不在同一坐标系中.
and you get your plot. Of course its not in the same coordinate system as the ggmap
background.
您可能可以使用基本R的 contourLines
函数来获取UTM坐标中的轮廓线坐标,制作一个 SpatialLinesDataFrame
,然后转换为Lat-long并添加到 ggmap
.
You can probably use base R's contourLines
function to get the coordinates of contour lines in UTM coordinates, make a SpatialLinesDataFrame
, then transform to Lat-long and add to a ggmap
.
获得看起来像网格图的另一种可能性是使用具有正方形的点作为形状.
Another possibility to get what looks like a grid map is to use points with squares as the shape.
ggmap(POLA.map) + geom_point(data = PM, mapping = aes(Longitude, Latitude, colour=Value), size=4, alpha=0.5, shape=15) + scale_colour_gradient(low = 'green', high = 'red')
有些伪像中的网格单元重叠,看起来有点像单元轮廓,并且图例显示时没有不透明,因此看起来比单元更饱和.您必须正确设置size参数,因为它取决于图形设备的大小.
There's some artefacts where the grid cells overlap that look a bit like cell outlines, and the legend is showing with no opacity so looks more saturated than the cells. You'll have to get the size parameter right, as it depends on the size of your graphics device.
所有操作失败,将数据转换为栅格数据包栅格对象,将其保存为GeoTIFF,然后将其加载到QGIS中,该QGIS可将UTM网格即时重新投影到Lat-long上.
Failing all that, turn your data into a raster package raster object, save it as a GeoTIFF, and load it into QGIS, which can reproject UTM grids onto Lat-long on the fly.
QGIS还具有一些不错的混合模式,因此您可以轻松地做到这一点:
QGIS also has some nice blending modes so you can do this pretty easily:
请注意,这不是透明性,这是乘法混合.透明度会导致深色被洗掉,而乘法混合则让黑色透出来,因此标签和底图细节仍然可见.
Note this is not transparency, this is multiplicative blending. Transparency causes dark colours to get washed out, whereas multiplicative blending lets black show through, so labelling and base map detail are still visible.
另外,请注意栅格是如何未与轴对齐的(尤其是在底部很明显).
Also, note how the raster is not axis-aligned (especially obvious at the bottom).
这篇关于使用ggmap在地图上绘制等高线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!