[英]R: Centering a map in ggplot2
I would like to center the outline of Jersey in the middle of the bounding box (See attached image below) 我想在边界框的中间居中放置泽西岛的轮廓(请参见下面的图片)
This is what the shapefile looks like before using the coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543))
这是使用coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543))
之前shapefile的外观
Code is as follows: 代码如下:
graph1<-ggplot()+
geom_polygon(data=middlestates,colour="black",fill="#D3D3D3",aes(x=long,y=lat,group=group))+
geom_polygon(data=df,colour="black",aes(x=long,y=lat,group=group,fill=ALG))+
ggtitle("Figure 2.2A: Assessment Results for\nGeneral Aquatic Life Use, Spatial Extent")+
xlab("")+
ylab("")+
coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543))+
ggsn::scalebar(df,location="bottomleft",dist = 50,st.dist=0.02,st.size=3, height=0.01)+
scale_fill_manual("Aquatic Life Designated\nUse 2014 Assessment",values=c((values=c(colors))))+
cowplot::background_grid(major= "none",minor = "none") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.text.y = element_blank(), axis.ticks.y = element_blank(),
axis.line = element_blank(),
panel.background = element_blank(),
legend.position=c(0.8,0.2),
panel.border = element_rect(fill=NA),
legend.background = element_blank(),
legend.text = element_text(size=12),
legend.title = element_text(colour="black", size=12, face="bold"),
plot.title=element_text(size=15, face="bold",hjust=0.5))
pdf(file="Figure 2.2A.pdf",width=13,height=12,paper="a4")
north2(graph1, x = 0.10, y = 0.89, scale = 0.1, symbol = 3)
dev.off()
Is there anyway to do this without changing the 无论如何有没有做这个做
coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543))
line?? 线?? I would like to keep the way the map looks. 我想保持地图的外观。 Any help or feedback would be greatly appreciated! 任何帮助或反馈将不胜感激! Thanks! 谢谢!
Here's a mockup of similar shapefiles. 这是类似shapefile的模型。 I'm using sf
, because it's great for quickly filtering or analyzing spatial data, works like dplyr
but for shapes, and because it plots easily with the newest version of ggplot2
(might need to use the github version). 我正在使用sf
,因为它非常适合快速过滤或分析空间数据,像dplyr
一样dplyr
但可以用于形状,并且因为它使用最新版本的ggplot2
可以轻松ggplot2
(可能需要使用github版本)。 If your shapefiles are in Spatial* formats, you can use st_as_sf
to create an sf
object. 如果shapefile是Spatial *格式,则可以使用st_as_sf
创建sf
对象。
To get an sf
of states and an sf
of New Jersey towns, I used functions from tigris
that download shapefiles from the Census. 为了得到一个sf
状态和sf
新泽西州的城镇,我使用的功能tigris
从人口普查是下载shape文件。 That was just the easiest way I had to get the shapefiles; 那只是我必须获得shapefile的最简单方法。 you use whatever ones you're already working with. 您可以使用已经使用的任何工具。
I filtered the mid_sf
object, which is an sf
of the states in the region, for just New Jersey, then piped it into st_buffer
to place a small buffer around it, then st_bbox
to get its bounding box. 我为新泽西州过滤了mid_sf
对象(该区域的状态的sf
,仅适用于新泽西州,然后将其输送到st_buffer
以在其周围放置一个小缓冲区,然后使用st_bbox
获得其边界框。
There are two plots: one without the coordinate limits set, and one with them set based on nj_bbox
. 有两个图:一个未设置坐标限制,另一个基于nj_bbox
设置。
library(tidyverse)
library(sf)
us_sf <- tigris::states(class = "sf", cb = T)
mid_sf <- us_sf %>%
filter(NAME %in% c("New York", "New Jersey", "Pennsylvania", "Connecticut", "Delaware", "Rhode Island", "Massachusetts", "Maryland", "Virginia", "West Virginia"))
nj_towns_sf <- tigris::county_subdivisions(state = "NJ", class = "sf", cb = T)
# dist is in degrees latitude & longitude
nj_bbox <- mid_sf %>%
filter(NAME == "New Jersey") %>%
st_buffer(dist = 0.15) %>%
st_bbox()
#> Warning in st_buffer.sfc(st_geometry(x), dist, nQuadSegs): st_buffer does
#> not correctly buffer longitude/latitude data
nj_bbox
#> xmin ymin xmax ymax
#> -75.70961 38.77852 -73.74398 41.50742
nj_map <- ggplot() +
geom_sf(data = mid_sf, color = "gray40", fill = "white", size = 0.5) +
geom_sf(data = nj_towns_sf, color = "gray20", fill = "tomato", alpha = 0.7, size = 0.25)
nj_map
nj_map +
coord_sf(ndiscr = 0, xlim = c(nj_bbox$xmin, nj_bbox$xmax), ylim = c(nj_bbox$ymin, nj_bbox$ymax))
Created on 2018-05-02 by the reprex package (v0.2.0). 由reprex包 (v0.2.0)创建于2018-05-02。
I think it should be something like this. 我认为应该是这样的。
library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
states <- map_data("state")
dim(states)
#> [1] 15537 6
head(states)
nj_df <- subset(states, region == "new jersey")
head(nj_df)
counties <- map_data("county")
nj_county <- subset(counties, region == "new jersey")
head(nj_county)
nj_base <- ggplot(data = nj_df, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")
nj_base + theme_nothing()
bj_base + theme_nothing() +
geom_polygon(data = nj_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA) # get the state border back on top
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.