绘制世界地图时,如何将各个值更改为图例中的一系列值?

问题描述 投票:0回答:1

我想将图例从单一值调整为值范围,并将颜色和大小组合在一起。

我的代码如下:

library(ggplot2)
library(dplyr)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)

merged_data_sf <- st_as_sf(merged_data, coords = c("longitude", "latitude"), crs = 4326)

world <- ne_countries(scale = "medium", returnclass = "sf")

color_breaks <- c(0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0)
colors <- c("#E31A1C", "#FF7F00", "#FDBF6F", "#E9E4A6", "#A4D4A9", "#B2DF8A", "#33A02C", "#1F78B4")

size_breaks <- c(0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0)
size_range <- c(1, 5)

p1 <- ggplot() +
  geom_sf(data = world, fill = "grey87", color = "white") + 
  geom_sf(data = merged_data_sf, aes(size = median_mafruit, color = median_mafruit), alpha = 0.7) +
  labs(
    title = "Data1",
    subtitle = "10 years",
    x = "Longitude (°)",
    y = "Latitude (°)"
  ) +
  scale_color_gradientn(
    colors = colors,
    breaks = color_breaks,
    values = scales::rescale(color_breaks),
    limits = c(min(color_breaks), max(color_breaks))
  ) +
  scale_size_continuous(
    breaks = size_breaks,
    range = size_range,
    limits = c(min(size_breaks), max(size_breaks))
  ) +
  coord_sf(
    xlim = c(-4.4833, 7.25),
    ylim = c(42.45, 47.21),
    expand = FALSE
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    plot.subtitle = element_text(size = 12, face = "italic"),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8),
    legend.key.size = unit(0.5, "cm"),
    legend.spacing.x = unit(0.5, "cm"),
    legend.spacing.y = unit(0.5, "cm")
  ) +
  guides(
    size = guide_legend(
      title = bquote("Yield (t·ha"^.(1)*")"),
      order = 1
    ),
    color = guide_legend(
      title = bquote("Yield (t·ha"^.(1)*")"),
      order = 1
    )
  )
print(p1)

输出图如下: enter image description here 但我想将图例调整为数值范围而不是单个值,并将颜色和大小组合在一起,如下所示:

enter image description here

部分数据集如下:

merged_data<-structure(list(Order = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
                         13, 14, 15, 16, 17, 18, 19, 20), latitude = c(43.2143, 43.3697, 
                                                                       43.3909, 43.3926, 43.3961, 43.3978, 43.066, 43.2215, 43.368, 
                                                                       43.435, 43.4434, 43.4623, 43.4895, 43.4856, 43.3738, 43.4761, 
                                                                       43.5102, 43.5118, 43.5062, 43.4933), longitude = c(-4.479, -4.4804, 
                                                                                                                          -4.4606, -4.4484, -4.4241, -4.412, -4.1046, -4.1049, -4.1031, 
                                                                                                                          -4.0818, -4.021, -3.9502, -3.8184, -3.7798, -3.7279, -3.7147, 
                                                                                                                          -3.5971, -3.5849, -3.5585, -3.5177), median_mafruit = c(2.73, 
                                                                                                                                                                                  1.095, 1.115, 2.73, 0.527, 0.527, 0.962, 1.039, 1.039, 2.73, 
                                                                                                                                                                                  2.73, 2.73, 2.73, 2.73, 0.544, 2.73, 2.73, 2.73, 0.478, 2.73)), row.names = c(NA, 
                                                                                                                                                                                                                                                                -20L), spec = structure(list(cols = list(Order = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                             "collector")), latitude = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                   "collector")), longitude = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                          "collector")), median_mafruit = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "tbl_df", "tbl", "data.frame"))

有任何建议欢迎提出!预先感谢!

r ggplot2 mapping legend geom-sf
1个回答
0
投票

我想这就是您要找的东西?

p1 <- ggplot() +
  geom_sf(data = world, fill = "grey87", color = "white") + 
  geom_sf(data = merged_data_sf, aes(size = median_mafruit, color = median_mafruit), alpha = 0.7) +
  labs(
    title = "Data1",
    subtitle = "10 years",
    x = "Longitude (°)",
    y = "Latitude (°)"
  ) +
  scale_color_gradientn(
    colors = colors,
    breaks = color_breaks,
    values = scales::rescale(color_breaks),
    limits = c(min(color_breaks), max(color_breaks)),
    labels = c("[0, 0.5)", "[0.5, 1.0)", "[1.0, 1.5)", "[1.5, 2.0)", 
               "[2.0, 2.5)", "[2.5, 3.0)", "[3.0, 3.5)", "[3.5, 4.0]")
  ) +
  scale_size_continuous(
    breaks = size_breaks,
    range = size_range,
    limits = c(min(size_breaks), max(size_breaks)),
    labels = c("[0, 0.5)", "[0.5, 1.0)", "[1.0, 1.5)", "[1.5, 2.0)", 
               "[2.0, 2.5)", "[2.5, 3.0)", "[3.0, 3.5)", "[3.5, 4.0]")
  ) +
  coord_sf(
    xlim = c(-4.4833, 7.25),
    ylim = c(42.45, 47.21),
    expand = FALSE
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    plot.subtitle = element_text(size = 12, face = "italic"),
    legend.title = element_text(size = 10),
    legend.text = element_text(size = 8),
    legend.key.size = unit(0.5, "cm"),
    legend.spacing.x = unit(0.5, "cm"),
    legend.spacing.y = unit(0.5, "cm")
  ) +
  guides(
    size = guide_legend(
      title = bquote("Yield (t·ha"^.(1)*")"),
      order = 1
    ),
    color = guide_legend(
      title = bquote("Yield (t·ha"^.(1)*")"),
      order = 1
    ),
    fill = guide_legend(
      title = bquote("Yield (t·ha"^.(1)*")"),
      override.aes = list(size = 4) 
    )
  )

print(p1)

输出如下: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.