Skip to content

Latitude and longitude labels for coord_sf #6265

Closed
@Breeze-Hu

Description

@Breeze-Hu

I'm not sure if what I'm experiencing is the same problem as this one, again with incomplete labeling of the four positions of latitude and longitude:

library(ggplot2)
library(sf)
library(rnaturalearth)

# Data:
coast <- ne_coastline(scale = 50, returnclass = "sf") 
crs_pr <- "+proj=longlat +lon_0=180 +datum=WGS84  +no_defs"

coast1 = coast |> 
  st_break_antimeridian(lon_0 = 180) |> 
  st_transform(crs_pr)

ggplot(coast1)+
  geom_sf()+
  coord_sf(expand = FALSE,
           xlim = c(st_bbox(coast1)['xmin'], st_bbox(coast1)['xmax']),
           label_axes = list(top = 'E',bottom = "E", left="N",right="N")
           )

The labeled line for latitude is not crossed with the panel boundary.
Image

When a little unreasonable adjustment is made (slightly limiting the range of x), everything works fine:

ggplot(coast1)+
  geom_sf()+
  coord_sf(expand = FALSE,
           xlim = c(st_bbox(coast1)['xmin']+5, st_bbox(coast1)['xmax']-5),
           label_axes = list(top = 'E',bottom = "E", left="N",right="N")
  )
Image It happens even with `expand = FALSE` set, which is weird.

Originally posted by @Breeze-Hu in #2985

This is a similar problem for raster plotting. Even without some projection transformations (compared to the previously mentioned geom_sf data).

library(ggplot2)
library(terra)
library(tidyterra)

#Example data downloaded from NOAA 
https://psl.noaa.gov/mddb2/makePlot.html?variableID=1570
or 
https://psl.noaa.gov/tmp/mddb2/_H5mqysfuv/subset.nc

data <- rast('Path')

ggplot()+
 geom_spatraster(data = data,aes(fill = sst))+
  scale_fill_viridis_c()+
 coord_sf(expand = FALSE,
          label_axes = list(top = 'E',bottom = "E", left="N", right="N"))

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions