Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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中结合分类填充和渐变填充_R_Ggplot2_Geospatial_Sf - Fatal编程技术网

在地理空间-R中结合分类填充和渐变填充

在地理空间-R中结合分类填充和渐变填充,r,ggplot2,geospatial,sf,R,Ggplot2,Geospatial,Sf,我试图在地图上填充一个组合的分类变量和连续变量。例如,在我下面的最小重现性示例中,假设我想显示每个县的KrispyKreme甜甜圈店的数量,这通常是一个连续变量,我想在梯度上填充。但我也有一些县禁止使用“-1”表示的KrispyKremes,还有一些县正在建设“-2”。我想用不同的颜色显示这些,而不是在渐变上映射。我的真实数据中也有NA --到目前为止,我所拥有的: library(sf) library(ggplot2) nc <- st_read(system.file("shape

我试图在地图上填充一个组合的分类变量和连续变量。例如,在我下面的最小重现性示例中,假设我想显示每个县的KrispyKreme甜甜圈店的数量,这通常是一个连续变量,我想在梯度上填充。但我也有一些县禁止使用“-1”表示的KrispyKremes,还有一些县正在建设“-2”。我想用不同的颜色显示这些,而不是在渐变上映射。我的真实数据中也有NA

--到目前为止,我所拥有的:

library(sf)
library(ggplot2)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc$Status<-rep(c(-2,-1,runif(8)), 10)

ggplot(nc) + 
  geom_sf(aes(fill=Status),color = "black") + 
  coord_sf(datum = NA) + 
  theme_minimal()

非常感谢您的帮助,我一整天都在做这件事。

您需要将连续变量分成不同的类别

library(sf)
library(ggplot2)
library(dplyr)

# Set seed for reproducibility
set.seed(122)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc$Status<-rep(c(-2,-1,runif(8)), 10)
我决定根据分位数来切割变量,如下所示

nc2 <- nc %>%
  mutate(Status2 = case_when(
    Status == -2 ~ "-2",
    Status == -1 ~ "-1",
    Status >= 0 & Status < 0.15 ~ "0 - 0.15",
    Status >= 0.15 & Status < 0.6 ~ "0.15 - 0.6",
    Status >= 0.6 & Status < 0.75 ~ "0.6 - 0.75",
    Status >= 0.75                ~ "0.75 - 0.91"
  ))

非常感谢。上面构造的
Status2
方式使其成为字符变量。我想画一个分类变量。下面的代码生成一个因子变量(
Status3
),并将其绘制在地图中。它起作用了

library(sf) 
library(ggplot2) 
library(dplyr)

nc <- st_read(system.file("shape/nc.shp", package="sf")) 
nc$Status<-rep(c(-2,-1,runif(8)), 10)

nc3 <- nc %>%
  mutate(Status3 = factor(ifelse(Status>0,1,0)))

ggplot(nc3) + 
  geom_sf(aes(fill=Status3),color = "black") + 
  coord_sf(datum = NA) + 
  theme_minimal()
库(sf)
图书馆(GG2)
图书馆(dplyr)
nc=0.4,1,0),
水平=c(0:1),
标签=c(“低于40%,”高于40%))
#打印因子变量:生成错误
ggplot(CS_mun_shp2)+
geom_sf(aes)(几何=几何,
填充=cs_大于40),
颜色=NA)

我的代码与上面的可复制示例之间的唯一区别是,我需要在
aes()
中指定
geometry
,否则会出现另一个错误

就是这个,谢谢。从技术上讲,这只是概念上的缺失。这是一个答案还是另一个问题,还是对先前答案的评论?也许值得一看StackOverflow指南:而且不太容易复制,但我确实有一些猜测值得尝试。1) 很明显,你已经在因子中添加了级别和标签;有没有可能他和其中一个玩得不好。也许可以尝试简化该步骤,并直接在ggplot中填充这些步骤。2) 该错误似乎表明ggplot正在查找“点”数据而不是线数据。我的猜测是,在sf帧中,这可能被定义为错误的数据类型。希望有帮助…显然,没有可重复性,无法直接诊断。
nc2 <- nc %>%
  mutate(Status2 = case_when(
    Status == -2 ~ "-2",
    Status == -1 ~ "-1",
    Status >= 0 & Status < 0.15 ~ "0 - 0.15",
    Status >= 0.15 & Status < 0.6 ~ "0.15 - 0.6",
    Status >= 0.6 & Status < 0.75 ~ "0.6 - 0.75",
    Status >= 0.75                ~ "0.75 - 0.91"
  ))
ggplot(nc2) + 
  geom_sf(aes(fill=Status2),color = "black") + 
  coord_sf(datum = NA) + 
  theme_minimal() +
  scale_fill_manual(values = c("blue", "yellow", viridis::viridis(4)))
library(sf) 
library(ggplot2) 
library(dplyr)

nc <- st_read(system.file("shape/nc.shp", package="sf")) 
nc$Status<-rep(c(-2,-1,runif(8)), 10)

nc3 <- nc %>%
  mutate(Status3 = factor(ifelse(Status>0,1,0)))

ggplot(nc3) + 
  geom_sf(aes(fill=Status3),color = "black") + 
  coord_sf(datum = NA) + 
  theme_minimal()
# plotting continuous variable: WORKS FINE
ggplot(CS_mun_shp)+
  geom_sf(aes(geometry=geometry,
              fill=ppc_sih),
          color=NA) 

# constructing factor variable
CS_mun_shp2 <- CS_mun_shp %>%
  mutate(cs_above40=factor(ifelse(ppc_sih>=0.4,1,0), 
                           levels=c(0:1), 
                           labels=c('below 40%','above 40%')))

# plotting factor variable: GENERATES ERROR  
ggplot(CS_mun_shp2)+
  geom_sf(aes(geometry=geometry,
              fill=cs_above40),
          color=NA)