Home > front end >  Is there a way to add tickmarkers and ticklabels to the right and top of the figure when plotting a
Is there a way to add tickmarkers and ticklabels to the right and top of the figure when plotting a

Time:09-29

everyone. I use the ggplot2 and sf library to plot the geographically relevant figure.

Here is the code.

library("ggplot2")
library("sf")
library("rnaturalearth")
# library("rnaturalearthdata")
us <- ne_countries(scale = "medium", returnclass = "sf")

# ref: https://github.com/tidyverse/ggplot2/issues/2090
bb <- st_sfc(
  st_polygon(list(cbind(
    c(-119, -74, -74, -119, -119), 
    c(22, 22, 51, 51, 22) 
  ))), crs = "WGS84") %>%
  st_transform(crs = st_crs("ESRI:102003")) # USA Contiguous Albers Equal Area Conic, also used for plotting the map

# Plot
ggplot()  
  geom_sf(data = us, fill = NA,color = "black",alpha=.9)  
  # Crop map with bounding box and reproject
  coord_sf(xlim = c(st_bbox(bb)["xmin"], st_bbox(bb)["xmax"]),
           ylim = c(st_bbox(bb)["ymin"], st_bbox(bb)["ymax"]),
           crs = st_crs("ESRI:102003"),
           expand = FALSE)  
  # scale_x_continuous(sec.axis=dup_axis()) 
  # scale_y_continuous(position = "right") 
  xlab(NULL)   ylab(NULL)  
theme_bw()

enter image description here

I want to add the tick markers and tick labels to the right and top of the output figure (red parts) so that all four sides of the figure are labeled. I googled and tried some methods:

scale_x_continuous(sec.axis=dup_axis()) 
# or 
scale_y_continuous(position = "right") 

but it did not work.

Do you have any ideas? Thanks in advance.

CodePudding user response:

I think you need coord_sf() with the label_axes parameter modified. From the help:

Character vector or named list of character values specifying which graticule lines (meridians or parallels) should be labeled on which side of the plot. Meridians are indicated by "E" (for East) and parallels by "N" (for North). Default is "--EN", which specifies (clockwise from the top) no labels on the top, none on the right, meridians on the bottom, and parallels on the left. Alternatively, this setting could have been specified with list(bottom = "E", left = "N").

See the result with label_axes = list(bottom = "E", top="E", left="N", right="N")):

library("ggplot2")
library("sf")
library("rnaturalearth")
# library("rnaturalearthdata")
us <- ne_countries(scale = "medium", returnclass = "sf")

# ref: https://github.com/tidyverse/ggplot2/issues/2090
bb <- st_sfc(
  st_polygon(list(cbind(
    c(-119, -74, -74, -119, -119),
    c(22, 22, 51, 51, 22)
  ))),
  crs = "WGS84"
) %>%
  st_transform(crs = st_crs("ESRI:102003")) # USA Contiguous Albers Equal Area Conic, also used for plotting the map

ggplot()  
  geom_sf(data = us, fill = NA, color = "black", alpha = .9)  
  # Crop map with bounding box and reproject
  coord_sf(
    xlim = c(st_bbox(bb)["xmin"], st_bbox(bb)["xmax"]),
    ylim = c(st_bbox(bb)["ymin"], st_bbox(bb)["ymax"]),
    crs = st_crs("ESRI:102003"),
    expand = FALSE,
    label_axes = list(
      bottom = "E", top = "E",
      left = "N", right = "N"
    )
  )  
  xlab(NULL)  
  ylab(NULL)  
  theme_bw()

enter image description here

  • Related