R 在ggplot2中显示长纬度代替东距和北距
我已经压缩了形状文件。 我想将xlables和ylables更改为等效的经度和纬度值,而不是东距和北距。请建议我如何显示纬度和经度来代替此北距和东距。当我将shapefile的投影更改为WGS1984时,它会起作用,但这会扭曲区域,我不想这样做。 我的代码是R 在ggplot2中显示长纬度代替东距和北距,r,ggplot2,latitude-longitude,raster,R,Ggplot2,Latitude Longitude,Raster,我已经压缩了形状文件。 我想将xlables和ylables更改为等效的经度和纬度值,而不是东距和北距。请建议我如何显示纬度和经度来代替此北距和东距。当我将shapefile的投影更改为WGS1984时,它会起作用,但这会扭曲区域,我不想这样做。 我的代码是 library(raster) library(RColorBrewer) library(ggplot2) library(ncdf) poly<-shapefile("Test_poly.shp", stringsAsFactor
library(raster)
library(RColorBrewer)
library(ggplot2)
library(ncdf)
poly<-shapefile("Test_poly.shp", stringsAsFactors=FALSE, verbose=FALSE)
ggplot(poly) +
aes(long,lat,group=group, fill = id) +
geom_polygon() +
geom_path(color="white") +
coord_equal() +
scale_fill_brewer("Bydeler")
库(光栅)
图书馆(RColorBrewer)
图书馆(GG2)
图书馆(ncdf)
poly(感谢您更新了shapefile。但是我还发现我需要在我的系统上安装proj和rgdal R包。也许值得一提?或者这只是反映了我在R中没有做很多空间工作的事实)
最终,我不认为ggplot2有一个内置系统,可以在轴上使用不同的映射单位。我在这里介绍的是一些解决办法,有些难看,但应该是可行的
要获得Lat和Long轴,您可以选择直接将shapefile转换为Lat和Long。例如:
poly <- shapefile("Test_poly.shp", stringsAsFactors=FALSE, verbose=FALSE)
poly <- spTransform(poly, CRS("+proj=longlat +ellps=GRS80"))
ggplot(poly2) +
aes(long,lat,group=group, fill = id) +
ylim(c())+
xlim(c(1324794,1341700)) +
geom_polygon() +
geom_path(color="white") +
coord_equal() +
scale_fill_brewer("Bydeler")
或者,您可以使用原始形状文件,然后使用手动轴标签来获得纬度和经度,例如,scale_y__连续(breaks=c(700000、800000、900001000000),labels=c(51,52,53,54))
这不是一个优雅的解决方案,但对于制作用于演示或发布的图形来说应该足够有效。您的链接是指向Test_poly.shp
,但shapefile是一组文件,因此Test_poly.dbf
,Test_poly.prg
,Test_poly.shp
,以及其他一些文件。您需要提供所有这些,否则代码将无法运行。另外,请查看scale\u x\u continuous
以更改轴上的标签。
> poly
class : SpatialPolygonsDataFrame
features : 7
extent : 1211747, 1466285, 728771.9, 999422.3 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=aea +lat_1=50 +lat_2=58.5 +lat_0=45 +lon_0=-126 +x_0=1000000 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0
variables : 1
names : Id
min values : 0
max values : 0