我试图绘制地图,但我无法弄清楚为什么以下不起作用:
这是一个最小的例子
testdf <- structure(list(x = c(48.97, 44.22, 44.99, 48.87, 43.82, 43.16, 38.96, 38.49, 44.98, 43.9), y = c(-119.7, -113.7, -109.3, -120.6, -109.6, -121.2, -114.2, -118.9, -109.7, -114.1), z = c(0.001216, 0.001631, 0.001801, 0.002081, 0.002158, 0.002265, 0.002298, 0.002334, 0.002349, 0.00249)), .Names = c("x", "y", "z"), row.names = c(NA, 10L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
这适用于1-8行:
ggplot(data = testdf[1,], aes(x,y,fill = z)) + geom_tile()
ggplot(data = testdf[1:8,], aes(x,y,fill = z)) + geom_tile()
Run Code Online (Sandbox Code Playgroud)
但不是9行:
ggplot(data = testdf[1:9,], aes(x,y,fill = z)) + geom_tile()
Run Code Online (Sandbox Code Playgroud)
最终,我正在寻找一种在非常规网格上绘制数据的方法.我使用geom_tile并不是必不可少的,但是点上的任何空间填充插值都可以.
testdf 上面是完整数据集的一小部分,是美国的高分辨率栅格(> 7500行)
require(RCurl) # requires libcurl; sudo apt-get install libcurl4-openssl-dev
tmp <- getURL("https://gist.github.com/raw/4635980/f657dcdfab7b951c7b8b921b3a109c7df1697eb8/test.csv")
testdf <- read.csv(textConnection(x))
Run Code Online (Sandbox Code Playgroud)
使用geom_point工作,但没有所需的效果:
ggplot(data = testdf, aes(x,y,color=z)) + geom_point()
Run Code Online (Sandbox Code Playgroud)如果我转换无论是 X或Y为矢量1:10,按预期工作的情节:
newdf <- transform(testdf, y =1:10)
ggplot(data = newdf[1:9,], aes(x,y,fill = z)) + geom_tile()
newdf <- transform(testdf, x =1:10)
ggplot(data = newdf[1:9,], aes(x,y,fill = z)) + geom_tile()
Run Code Online (Sandbox Code Playgroud)sessionInfo()R version 2.15.2 (2012-10-26) Platform: x86_64-pc-linux-gnu (64-bit)
> attached base packages: [1] stats graphics grDevices utils
> datasets methods base
> other attached packages: [1] reshape2_1.2.2 maps_2.3-0
> betymaps_1.0 ggmap_2.2 ggplot2_0.9.3
> loaded via a namespace (and not attached): [1] colorspace_1.2-0
> dichromat_1.2-4 digest_0.6.1 grid_2.15.2
> gtable_0.1.2 labeling_0.1 [7] MASS_7.3-23
> munsell_0.4 plyr_1.8 png_0.1-4
> proto_0.3-10 RColorBrewer_1.0-5 [13] RgoogleMaps_1.2.0.2
> rjson_0.2.12 scales_0.2.3 stringr_0.6.2
> tools_2.15.2
Run Code Online (Sandbox Code Playgroud)
Sim*_*lon 11
您不能使用的原因geom_tile()(或者更合适geom_raster()的原因是因为这两个geoms依赖于您的瓷砖均匀间隔,而不是它们.您需要将数据强制转换为点,并将这些数据重新采样为均匀间隔的栅格,您可以然后绘制geom_raster().你将不得不接受你需要稍微重新采样原始数据,以便根据需要绘制.
你也应该在阅读了raster:::projection和rgdal:::spTransform对地图投影的更多信息.
require( RCurl )
require( raster )
require( sp )
require( ggplot2 )
tmp <- getURL("https://gist.github.com/geophtwombly/4635980/raw/f657dcdfab7b951c7b8b921b3a109c7df1697eb8/test.csv")
testdf <- read.csv(textConnection(tmp))
spdf <- SpatialPointsDataFrame( data.frame( x = testdf$y , y = testdf$x ) , data = data.frame( z = testdf$z ) )
# Plotting the points reveals the unevenly spaced nature of the points
spplot(spdf)
Run Code Online (Sandbox Code Playgroud)

# You can see the uneven nature of the data even better here via the moire pattern
plot(spdf)
Run Code Online (Sandbox Code Playgroud)

# Make an evenly spaced raster, the same extent as original data
e <- extent( spdf )
# Determine ratio between x and y dimensions
ratio <- ( e@xmax - e@xmin ) / ( e@ymax - e@ymin )
# Create template raster to sample to
r <- raster( nrows = 56 , ncols = floor( 56 * ratio ) , ext = extent(spdf) )
rf <- rasterize( spdf , r , field = "z" , fun = mean )
# Attributes of our new raster (# cells quite close to original data)
rf
class : RasterLayer
dimensions : 56, 135, 7560 (nrow, ncol, ncell)
resolution : 0.424932, 0.4248191 (x, y)
extent : -124.5008, -67.13498, 25.21298, 49.00285 (xmin, xmax, ymin, ymax)
# We can then plot this using `geom_tile()` or `geom_raster()`
rdf <- data.frame( rasterToPoints( rf ) )
ggplot( NULL ) + geom_raster( data = rdf , aes( x , y , fill = layer ) )
Run Code Online (Sandbox Code Playgroud)

# And as the OP asked for geom_tile, this would be...
ggplot( NULL ) + geom_tile( data = rdf , aes( x , y , fill = layer ) , colour = "white" )
Run Code Online (Sandbox Code Playgroud)

当然,我应该补充一点,这些数据毫无意义.您真正必须做的是获取SpatialPointsDataFrame,为其指定正确的投影信息,然后通过spTransform转换为latlong坐标,然后对转换后的点进行rasterzie.您真的需要有关于栅格数据的更多信息.你在这里有一个近似的近似,但最终它不是数据的真实反映.
这不是geom_tile()问题的答案,而是另一种绘制数据的方法.
由于您有x和y坐标30 km网格(我假设该网格的中间),那么您可以使用geom_point()和绘制数据.您应该选择适当的shape=值.形状15将绘制矩形.
另一个问题是x和y值 - 当绘制数据时,它们应该被绘制为x=y并且y=x对应于纬度和经度.
coord_equal() 将确保有一个正确的宽高比(我发现这个解决方案的比率作为网上的例子).
ggplot(data = testdf, aes(y,x,colour=z)) + geom_point(shape=15)+
coord_equal(ratio=1/cos(mean(testdf$x)*pi/180))
Run Code Online (Sandbox Code Playgroud)
