全世界最难的r语言代码 求懂哥帮看

想要处理https://mpjashby.github.io/crimemappingdata/london_crimes.zip和https://data.london.gov.uk/download/london_boroughs/9502cdec-5df0-46e3-8aa1-2b5c5233a31f/London_Boroughs.gpkg这两个网页的数据集,然后绘制一个地图来表明哪个地区犯罪率最高。下面这段代码一直报错,求大佬帮看!!!万分感谢

library(sf)
library(ggrepel)
library(gt)
library(rvest)
library(tidyverse)
library(ggplot2)
library(ggspatial)
library(sfhotspot)
library(readr)
library(dplyr)

# Define file paths
london_crimes_file <- tempfile(fileext = ".zip")
london_crimes_dir <- file.path(tempdir(), "london_crimes")

# Download the zip file
download.file(
  url = "https://mpjashby.github.io/crimemappingdata/london_crimes.zip", 
  destfile = london_crimes_file
)

# Unzip the file
unzip(london_crimes_file, exdir = london_crimes_dir)

# Read the shapefile
london_crimes <- read_sf(file.path(london_crimes_dir))
str(london_crimes$longitude)
str(london_crimes$latitude)

# 将经度和纬度列转换为数值型
london_crimes <- london_crimes %>%
  mutate(
    longitude = as.numeric(longitude),
    latitude = as.numeric(latitude)
  )

# 创建点对象并将其转换为地理空间几何对象
london_crimes <- london_crimes %>%
  st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
head(london_crimes)


Boroughs <- read_sf("https://data.london.gov.uk/download/london_boroughs/9502cdec-5df0-46e3-8aa1-2b5c5233a31f/London_Boroughs.gpkg")
head(Boroughs)

Boroughs <- st_transform(Boroughs, st_crs(london_crimes))

geom_hectares <- st_join(
  london_crimes, 
  Boroughs, 
  join = st_intersects
)

head(geom_hectares)

geom_map <- geom_hectares |> 
  count(geometry) |> 
  mutate(geometry = fct_reorder(geometry, n)) |>
  
    ggplot() +
  # Base map
  annotation_map_tile(type = "cartolight", zoomin = 1, progress = "none") +
  geom_sf(
    aes(fill = n ),
    data = geom_hectares,
    inherit.aes = FALSE,
    alpha = 0.75,
    size = 0.25
  ) +
  geom_sf(
    data = geom_hectares,
    inherit.aes = FALSE,
    colour = "black",
    fill = NA,
    size = 0.75
  ) +
  # Labels for 10 divsions with highest rates
  geom_sf_label(
    aes(label = str_wrap(label, width = 12)),
    data = geom_hectares,
    inherit.aes = FALSE,
    alpha = 0.75,
    size = 2.5,
    lineheight = 0.95,
    label.size = NA,
    label.padding = unit(0.1, "lines")
  ) +
  scale_fill_distiller(palette = "Oranges", direction = 1) +
  labs(
    title = "Stalking risk is highest in some rural QLD areas",
    subtitle = str_glue(
      "Ten police divisions with the highest stalking incidence are ",
      "highlighted"
    ),
    caption = str_glue(
      "Map produced by Matt Ashby on 31 Jan 2024\nCrime data from ",
      "https://www.police.qld.gov.au/maps-and-statistics"
    ),
    fill = "stalking incidents\nper 10,000 people\nin 2018"
  ) +
  theme_void() +
  theme(
    legend.background = element_rect(colour = NA, fill = rgb(1, 1, 1, 0.75)),
    legend.justification = c(1, 1),
    legend.margin = margin(6, 9, 9, 9),
    legend.position = c(0.95, 0.98),
    legend.title = element_text(size = 10),
    plot.caption = element_text(hjust = 0),
    plot.subtitle = element_text(margin = margin(6, 0, 6, 0)),
    plot.title = element_text(face = "bold", size = 16)
  )

geom_map

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值