overlap及两层地图

 library(sf)

library(tidyverse)

library(magrittr)

`%nin%` = Negate(`%in%`)


ts <- st_read("C:\\Users\\ruiying\\Desktop\\甘肃乡镇\\gansu.TAB") %>% 

  filter(str_detect(地区编码,"^620502") | str_detect(地区编码,"^620503")) %>%

  rename(code=地区编码) %>% 

  filter(row_number() %nin% c(44,45))


# ts %>% 

#   st_transform(4326)


# ts <- st_read("C:\\Users\\ruiying\\Desktop\\ts.geojson")


# bl <- read.csv("C:\\Users\\ruiying\\Desktop\\报告卡2021-11-03(8时).csv")


# bl %<>%

#   mutate(xiang=str_sub(bl$现住详细地址,10,12)) %>%

#   group_by(xiang) %>%

#   summarise(n=n())


xiang <- read.csv("C:\\Users\\ruiying\\Desktop\\xiang.csv") %>%

  select(-X)


ts %>% 

  left_join(xiang,by=c("NAME"="NAME")) %>% 

  ggplot() +

  geom_sf(aes(fill=n))+

  scale_fill_gradientn(breaks = c(2,4,6),

                       colors=c("gray", "yellow", "orange", "red"))+

  coord_sf(xlim = c(5862783,5946277),ylim = c(2918537,3000000))+

  geom_text(aes(label=NAME))

  

ts %<>% 

  left_join(xiang,by=c("NAME"="NAME")) %>% 

  mutate(n=replace_na(n,0))


ts %>% 

  ggplot() +

  geom_sf(aes(fill=factor(n)))+

  scale_fill_manual(values = c("white","gray","green","lightblue", "yellow", "orange", "red"))+

  coord_sf(xlim = c(5862783,5946277),ylim = c(2918537,3000000))+

  geom_sf_text(aes(label = NAME), colour = "black",size = 2,alpha=0.9,nudge_x = 0.025, nudge_y = -0.025)+

  theme_bw()


ts %>% 

  ggplot() +

  geom_sf(aes(fill=factor(n)))+

  scale_fill_manual(values = c("white","gray","green","lightblue", "yellow", "orange", "red"))+

  coord_sf(xlim = c(5862783,5946277),ylim = c(2918537,3000000))+

  geom_sf_label(aes(label = NAME), colour = "black",size = 2,alpha=0.9,nudge_x = 0.025, nudge_y = -0.025)+

  theme_bw()


ts %>%

  ggplot() +

  geom_sf(aes(fill=factor(n)))+

  scale_fill_manual(values = c("white","gray","green","lightblue", "yellow", "orange", "red"))+

  coord_sf(xlim = c(5862783,5946277),ylim = c(2918537,2989537))+

  ggrepel::geom_text_repel(aes(label = NAME, geometry = geometry),

                           stat = "sf_coordinates",

                           min.segment.length = 0,

                           max.overlaps = Inf)


ts %>%

  ggplot() +

  geom_sf(aes(fill=factor(n)))+

  # scale_fill_manual(values = c("white","gray","green","lightblue", "yellow", "orange", "red"))+

  coord_sf(xlim = c(5862783,5946277),ylim = c(2918537,2989537))+

  statebins::theme_statebins()

  ggrepel::geom_text_repel(aes(label = NAME, geometry = geometry),

                           stat = "sf_coordinates",

                           min.segment.length = 1,

                           max.overlaps = Inf)


  ggrepel::geom_label_repel(

    aes(label = NAME, geometry = geometry),

    stat = "sf_coordinates",

    min.segment.length = 1,

    max.overlaps = getOption("ggrepel.max.overlaps", default = 30)

  )


  

ts %>%

  arrange(n) %>% 

  unite('name_n',c(NAME,n),remove = F) %>% 

  filter(n>0) -> ts2


ggplot(ts) +

    geom_sf(fill='white',size=0.1)+

    geom_sf(data=ts2,aes(fill=factor(name_n)))+

    statebins::theme_statebins()+

    theme(legend.title = element_blank()) 



scale_fill_manual(values = c("white","gray","green","lightblue", "yellow", "orange", "red"))

  

ggplot(ts)+

    geom_sf()

    coord_sf(xlim = c(5862783,5946277),ylim = c(2918537,2989537))

  

评论

此博客中的热门博文

V2ray websocket(ws)+tls+nginx分流

Rstudio 使用代理