Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot2打印形状文件_R_Ggplot2_Shapefile - Fatal编程技术网

R 使用ggplot2打印形状文件

R 使用ggplot2打印形状文件,r,ggplot2,shapefile,R,Ggplot2,Shapefile,我想在一个绘图中创建一个具有九个(在本例中只有三个)贴图的镶嵌面。当我从原始的shapefile中选择subset时,我几乎成功地绘制了一张理想的地图。然而,当我试图同时绘制它们时,这是不可能的 绘图需要有相同的图例(离散数字作为值1、2、3、4、5),即使某些地图只有1到4之间的值 此外,当其中一个多边形缺少数据时,应使用图例NA值以灰色绘制 下面代码的输出示例位于底部。提供了一个示例数据 path如今,使用sf软件包来绘制所需的地图类型可能更容易。你可以在这里看到一些例子 我从那里改编了一个

我想在一个绘图中创建一个具有九个(在本例中只有三个)贴图的镶嵌面。当我从原始的
shapefile
中选择
subset
时,我几乎成功地绘制了一张理想的地图。然而,当我试图同时绘制它们时,这是不可能的

绘图需要有相同的图例(离散数字作为值
1、2、3、4、5
),即使某些地图只有
1
4
之间的值

此外,当其中一个多边形缺少数据时,应使用图例
NA值
以灰色绘制

下面代码的输出示例位于底部。提供了一个示例数据


path如今,使用
sf
软件包来绘制所需的地图类型可能更容易。你可以在这里看到一些例子

我从那里改编了一个示例,它展示了如何使用
ggplot2
及其
facet\u wrap
函数为给定变量的每个级别创建映射

例如,如果您已经有了一个具有一定级别的变量,则此处显示的某些步骤可能不是必需的

library(sf)
library(ggplot2)
library(tidyr)
library(dplyr)
library(classInt)
library(viridis)

# Read example shapefile from sf package
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# subset columns of interest as well as geometry column
# create BIR in which the variables BIR74, BIR79, NWBIR79
# become different levels of it
nc2 <- nc %>% select(BIR74, BIR79, NWBIR79, geometry) %>% gather(VAR, BIR, -geometry)

# HEre i just wanted to create 5 categories for the BIR variable
ints <- classIntervals(nc2$BIR, n = 5, style = "jenks")
nc2 <- nc2 %>% mutate(BIR_cat = cut(BIR, ints$brks, dig.lab=10)) 

# I just changed the levels's labels to match the output you are looking for
nc2 <- nc2 %>% mutate(values = ifelse(BIR_cat == "(3,1946]", "1", 
                                ifelse(BIR_cat == "(1946,4706]", "2", 
                                 ifelse(BIR_cat == "(4706,9087]", "3",
                                  ifelse(BIR_cat == "(9087,16184]", "4",
                                    ifelse(BIR_cat == "(16184,30757]", "5", NA))))))

# Map the spatial data
ggplot() +
 geom_sf(data = nc2, aes(fill = values)) +
 facet_wrap(~VAR, ncol = 1) +
 scale_fill_viridis(discrete=TRUE) 
库(sf)
图书馆(GG2)
图书馆(tidyr)
图书馆(dplyr)
图书馆(classInt)
图书馆(绿色)
#阅读sf包中的示例shapefile
nc%聚集(变量、BIR、-几何体)
#这里我只想为BIR变量创建5个类别

ints Hey,但是文件
nc.shp
的结构如何?我这样问是为了用我掌握的数据完全复制你的答案。嘿,我发布的示例是完全可复制的,如果你安装并加载所需的软件包,你可以检查
nc.shp
的结构。正如我所说,我建议使用
sf
包来处理R中的空间数据。无论如何,您可以在代码中使用
facet\u wrap()
来创建多个绘图,每个级别的变量对应一个绘图。我还建议大家阅读。
library(sf)
library(ggplot2)
library(tidyr)
library(dplyr)
library(classInt)
library(viridis)

# Read example shapefile from sf package
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# subset columns of interest as well as geometry column
# create BIR in which the variables BIR74, BIR79, NWBIR79
# become different levels of it
nc2 <- nc %>% select(BIR74, BIR79, NWBIR79, geometry) %>% gather(VAR, BIR, -geometry)

# HEre i just wanted to create 5 categories for the BIR variable
ints <- classIntervals(nc2$BIR, n = 5, style = "jenks")
nc2 <- nc2 %>% mutate(BIR_cat = cut(BIR, ints$brks, dig.lab=10)) 

# I just changed the levels's labels to match the output you are looking for
nc2 <- nc2 %>% mutate(values = ifelse(BIR_cat == "(3,1946]", "1", 
                                ifelse(BIR_cat == "(1946,4706]", "2", 
                                 ifelse(BIR_cat == "(4706,9087]", "3",
                                  ifelse(BIR_cat == "(9087,16184]", "4",
                                    ifelse(BIR_cat == "(16184,30757]", "5", NA))))))

# Map the spatial data
ggplot() +
 geom_sf(data = nc2, aes(fill = values)) +
 facet_wrap(~VAR, ncol = 1) +
 scale_fill_viridis(discrete=TRUE)