I want to add labels at specific values along the two axes using ggplot and an overlaying ggmap. Only the extreme values of the x axis are shown.
Hi, I have prepared the following, reproducible example in R using ggplot2, ggmap and ggspatial functions:
library("ggplot2")
library("ggmap")
library("ggspatial")
register_stadiamaps("f6528bd0-7cc4-4ba1-8b78-053d0082ec72")
xmin <- -8.93; xmax <- -7.80; ymin <- 37.23; ymax <- 39.10;
pt <- c( left = xmin, bottom = ymin, right = xmax, top = ymax );
PT_map <- get_stadiamap(pt, zoom = 10, maptype = "stamen_terrain") %>% ggmap();
breaks_x <- c(-8.9, -11.2, -13.4, -15.6, -7.8);
breaks_y <- c(37.2, 46.5, 55.8, 65.1, 39.1);
north_arrow_gg <- ggspatial::annotation_north_arrow(location = "tl",
pad_x = unit(0.1, "in"), pad_y = unit(0.1, "in"),
height = unit(2.0, "cm"), width = unit(2.0, "cm"),
style = ggspatial::north_arrow_nautical(fill = c("grey40", "white"),
line_col = "grey20",text_family = "ArcherPro Book"))
gg_test <- PT_map + scale_x_continuous(breaks = breaks_x, limits = c(xmin, xmax) ) +
scale_y_continuous(breaks = breaks_y, limits = c(ymin, ymax) ) +
north_arrow_gg +
annotation_scale(location = "bl", width_hint = 0.5, line_width = 1) +
coord_sf(crs = 4326, expand = FALSE )
gg_test
that leads to the following map:
What I would like to have is for the axis labels to appera based on the breaks_x and breaks_y vectors.
It seems that using scale_x_continuous and scale_y_continuous is replacing your scale with the defined breaks. Simply removing these will generate the map with the required breaks you need. Please try the below code.
library("ggplot2")
library("ggmap")
library("ggspatial")
register_stadiamaps("f6528bd0-7cc4-4ba1-8b78-053d0082ec72")
xmin <- -8.93; xmax <- -7.80; ymin <- 37.23; ymax <- 39.10;
pt <- c( left = xmin, bottom = ymin, right = xmax, top = ymax );
PT_map <- get_stadiamap(pt, zoom = 10, maptype = "stamen_terrain") %>% ggmap();
breaks_x <- c(-8.9, -11.2, -13.4, -15.6, -7.8);
breaks_y <- c(37.2, 46.5, 55.8, 65.1, 39.1);
north_arrow_gg <- ggspatial::annotation_north_arrow(location = "tl",
pad_x = unit(0.1, "in"), pad_y = unit(0.1, "in"),
height = unit(2.0, "cm"), width = unit(2.0, "cm"),
style = ggspatial::north_arrow_nautical(fill = c("grey40", "white"),
line_col = "grey20"))
gg_test <- PT_map +
north_arrow_gg +
annotation_scale(location = "bl", width_hint = 0.5, line_width = 1) +
coord_sf(crs = 4326, expand = FALSE )
gg_tes
t