rggplot2geo

How to set the colors for a numeric variable and color points above a threshold in one color?


I have the following code:

library(ggrepel)
library(ggOceanMaps)
library(ggspatial)
library(readr)

map <- basemap(c(-180, 180, -90, 90), bathymetry = TRUE)+
  ggspatial::geom_spatial_point(
    data = daten_neu, aes(x = as.numeric(Longitude), y = as.numeric(Latitude), color = as.numeric(Neogloboquadrina_pachyderma...35))
  ) 

print(map)

Using these Data:

   Latitude Longitude Neogloboquadrina_pachyderma...35
   <chr>    <chr>     <chr>                           
 1 8.983    51.733    1                               
 2 44.3498  -30.267   8                               
 3 50.9332  -41.75    602                              
 4 36.1333  -68.9167  1                               
 5 35.7833  -68.9333  1                               
 6 37.65    -72.95    62                               
 7 35.7667  -69.0833  1                               
 8 39.1332  -42.65    2                               
 9 5.2833   -17.0667  1                               
10 3.65     -18.3     14   

With this I display the figure enter image description here

However, I have several questions that I can't answer by searching the web. 1. change as.numeric() so that I can set that the range stops at 300 and everything above it is only displayed as the top colour

  1. how can I change the colour from dark blue (low) to red (high)?

Solution

  • The legend can be controlled via the scale_xxx_xxx. As you are mapping on the color aesthetic it's scale_color_xxx and as you have a continuous variable it's scale_color_continuous. However, as you want a gradient from red to blue it's better to switch to scale_color_gradient.

    Next, while you can recode your data one option to have the same color for points above a threshold would be to set the limits of the scale and use oob = scales::oob_squish:

    library(ggOceanMaps)
    library(ggspatial)
    
    basemap(c(-180, 180, -90, 90), bathymetry = TRUE) +
      ggspatial::geom_spatial_point(
        data = daten_neu, aes(
          x = as.numeric(Longitude),
          y = as.numeric(Latitude),
          color = as.numeric(Neogloboquadrina_pachyderma...35)
        )
      ) +
      scale_color_gradient(
        name = "Neogloboquadrina pachyderma",
        breaks = seq(0, 300, 100),
        labels = c(seq(0, 250, 100), "300 and more"),
        low = "darkblue", high = "darkred",
        limits = c(NA, 300),
        oob = scales::oob_squish
      )
    

    enter image description here

    DATA

    # run e.g. dput(head(date_neu, 10))
    daten_neu <- structure(list(Latitude = c(
      8.983, 44.3498, 50.9332, 36.1333,
      35.7833, 37.65, 35.7667, 39.1332, 5.2833, 3.65
    ), Longitude = c(
      51.733,
      -30.267, -41.75, -68.9167, -68.9333, -72.95, -69.0833, -42.65,
      -17.0667, -18.3
    ), Neogloboquadrina_pachyderma...35 = c(
      1L, 8L,
      602L, 1L, 1L, 62L, 1L, 2L, 1L, 14L
    )), class = "data.frame", row.names = c(
      "1",
      "2", "3", "4", "5", "6", "7", "8", "9", "10"
    ))