使用ggspatial显示两个数据集数据

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

您好,我想用 ggspatial 绘制北极地区的数据,为此我使用代码

library(ggOceanMaps)
library(readxl)
library(ggspatial)
library(dplyr)
    data_a <- structure(list(Nr = c(1, 2, 3, 4, 5, 6), Name = c("MD95-2006", 
"IODP 302", "IODP 302", "IODP 302", "IODP 302", "IODP 302"), 
    Lat = c(57.083333, 87.89, 87.9036, 87.92118, 87.93333, 87.86658
    ), Long = c(-8.05, 137.65, 138.46065, 139.365501, 139.535, 
    136.17735), `18O` = c(0.69, NA, NA, NA, NA, NA), Info = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), Source = c("MD95-2006 planktic foraminifera ?13C and ?18O", 
    "https://www.ecord.org/expedition302/", "https://www.ecord.org/expedition302/", 
    "https://www.ecord.org/expedition302/", "https://www.ecord.org/expedition302/", 
    "https://www.ecord.org/expedition302/")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))


data_b <- structure(list(Nr = c(1, 2, 3, 4, 5, 6), Name = c("Simstich", 
"Schiebel", "Schiebel", "Stangeew", "Stangeew", "Stangeew"), 
    Lat = c(75.003333, 62.50275, 67.225033, 56.2747, 53.4347, 
    52.874), Long = c(-7.313333, -13.99235, 2.920317, -48.6992, 
    -50.0673, -51.5128), `18O` = c(NA, NA, NA, NA, NA, NA), Info = c("data for different depths", 
    NA, NA, NA, NA, NA), Source = c("https://doi.pangaea.de/10.1594/PANGAEA.82001?format=html#download", 
    "https://doi.pangaea.de/10.1594/PANGAEA.75647?format=html#download", 
    "https://doi.pangaea.de/10.1594/PANGAEA.75719", "https://doi.pangaea.de/10.1594/PANGAEA.706908", 
    "https://doi.pangaea.de/10.1594/PANGAEA.706908", "https://doi.pangaea.de/10.1594/PANGAEA.706908"
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

data <- bind_rows(list(data_a,data_b), .id="data_source")


map <- basemap(limits = 60, bathy.style = "rcb")+ 
  ggspatial::geom_spatial_point(data = data, aes(x = Long, y = Lat), color = "red")
print(map)

但是,我只能显示一组数据,如何通过颜色区分data_a和data_b?不幸的是我在文档中找不到任何内容。

我想到了类似的事情:

ifelse(data$data_source == 1, "darkblue","darkgreen"
r geospatial
1个回答
0
投票

正如我在评论中已经提到的,您可以通过将

data_source
映射到
color
美学来实现您想要的结果。之后,您可以使用
scale_color_manual
设置所需的颜色(此外,您可以通过
labels=
参数提供标签)。我遇到的一个问题是,默认情况下,图例中的彩色点下方放置了一个深灰色矩形,使您的自定义颜色几乎不可见。我找到解决这个问题的唯一选择是通过
override.aes
guide_legend
参数删除填充颜色。

library(ggOceanMaps)
library(ggspatial)
library(ggplot2)

basemap(limits = 60, bathy.style = "rcb") +
  ggspatial::geom_spatial_point(
    data = data, aes(x = Long, y = Lat, color = data_source)
  ) +
  scale_color_manual(
    values = c("darkblue", "darkgreen")
  ) +
  guides(color = guide_legend(override.aes = list(fill = NA)))

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