Hello I would like to plot data in the north polar area with ggspatial, for this I use the code
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)
However, I can only display one data set, how can I differentiate between data_a and data_b by colour? unfortunately I can’t find anything in the documentation.
I Thought about something like:
ifelse(data$data_source == 1, "darkblue","darkgreen"
>Solution :
As I already mentioned in my comment you could achieve your desired result by mapping data_source on the color aesthetic. Afterwards you can use scale_color_manual to set your desired colors (additionally you can provide labels via the labels= argument). One issue I encountered was that by default a dark grey rectangle was placed underneath the colored points in the legend making your custom colors hardly visible. Only option I have to found to fix that was to remove the fill color via the override.aes argument of 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)))
