Search code examples
rggplot2ggspatial

Add label to the data


Hi I would like to plot a map section from the N Atlantic with this code! Therefore I use this 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
  )
), 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)), 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 = c(-60, -35, 62, 57), glaciers = TRUE,shapefiles = "Arctic",   bathy.style = "rcb") +
  ggspatial::geom_spatial_point(
    data = data, aes(x = Long, y = Lat, color = data_source)
  ) +
  scale_color_manual(
    values = c("red", "yellow")
  ) +
  guides(color = guide_legend(override.aes = list(fill = NA)))
print(map)

Now I want to set the information Name = c("...") to the data points. How can I achieve this? The labels should not have a white border and should not be in a box.


Solution

  • You can use ggspatial::geom_spatial_text(_repel) to add your labels. In the code below I opted for _repel as it automatically shifts the labels to avoid overlapping labels and labels overlapping the data points:

    library(ggOceanMaps)
    library(ggspatial)
    library(ggrepel)
    
    basemap(limits = 40, glaciers = TRUE, shapefiles = "Arctic", bathy.style = "rcb") +
      ggspatial::geom_spatial_point(
        data = data, aes(x = Long, y = Lat, color = data_source)
      ) +
      ggspatial::geom_spatial_text_repel(
        data = data, aes(x = Long, y = Lat, label = Name)
      ) +
      scale_color_manual(
        values = c("red", "yellow")
      ) +
      guides(color = guide_legend(override.aes = list(fill = NA)))
    

    enter image description here