dieghernan / tidyterra

tidyverse and ggplot2 methods for terra spatial objects

Home Page:https://dieghernan.github.io/tidyterra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

X- and Y- axis tick marks

DidDrog11 opened this issue · comments

Not sure if this is within the scope of this package but I've not had much luck trying to find other methods.

I am facetting several plots and then plotting them as a grid using cowplot::plot_grid(). This gets me what I want but there is an issue with the number of tick marks. I'd like to reduce the numbers of ticks on each sublot (to say 2) but not having much luck with scale_x_continuous(n.breaks = 2, breaks = waiver()) which I think should work. Using scales::breaks_pretty(n = 2)) throws up an error Error in min(lon) : invalid 'type' (closure) of argument which I think is because of the structure that is fed into the ggplot(data).

image

The code for each row is currently done in a for loop which is then fed to plot_grid()

for(i in 1:length(grids_with_traps))  {
  grids_plot[[i]] <- ggplot() + 
    geom_spatraster_rgb(data = bg[[i]]) +
    geom_sf(data = grids_with_traps[[i]] %>%
              mutate(landuse = str_to_title(landuse)),
            aes(fill = tn, colour = tn)) +
    coord_sf(expand = FALSE) +
    scale_colour_viridis_c(limits = c(0, 100)) +
    scale_fill_viridis_c(limits = c(0, 100)) +
    scale_x_continuous(breaks = scales::breaks_pretty(n = 2)) +
    guides(colour = "none") +
    facet_wrap(~ landuse) +
    labs(fill = "Number Trap-Nights",
         title = str_to_title(unique(grids_with_traps[[i]]$village))) +
    theme_bw() +
    theme(legend.position = "none")
}

A reproducible example may be easier using nc, neither of these do what I think they should.

ggplot() + 
  geom_sf(data = nc %>%
            filter(CNTY_ID %in% c(1825, 1827)),
          aes(fill = AREA, colour = AREA)) +
  coord_sf(expand = FALSE) +
  scale_colour_viridis_c(limits = c(0, 100)) +
  scale_fill_viridis_c(limits = c(0, 100)) +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 2)) +
  guides(colour = "none") +
  facet_wrap(~ CNTY_ID) +
  labs(fill = "AREA") +
  theme_bw() +
  theme(legend.position = "none")`
ggplot() + 
  geom_sf(data = nc %>%
            filter(CNTY_ID %in% c(1825, 1827)),
          aes(fill = AREA, colour = AREA)) +
  coord_sf(expand = FALSE) +
  scale_colour_viridis_c(limits = c(0, 100)) +
  scale_fill_viridis_c(limits = c(0, 100)) + 
  scale_x_continuous(n.breaks = 2, breaks = waiver()) +
  guides(colour = "none") +
  facet_wrap(~ CNTY_ID) +
  labs(fill = "AREA") +
  theme_bw() +
  theme(legend.position = "none")

Perfect, thanks.