paleolimbot / ggspatial

Enhancing spatial visualization in ggplot2
https://paleolimbot.github.io/ggspatial
365 stars 34 forks source link

ggspatial reorders facets #108

Open lucas-johnson opened 1 year ago

lucas-johnson commented 1 year ago

Thanks for an awesome package! I use this stuff all the time.

It appears that ggspatial reorders facets (alphabetically), which have a specific order as determined by the factor levels. It should maintain the facet ordering, especially when factors are used.

library(ggspatial)
packageVersion("ggspatial")
#> [1] '1.1.5'
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.2
packageVersion("ggplot2")
#> [1] '3.3.6'

data <- data.frame(x = 0:4, y = -(0:4))
data <- dplyr::bind_rows(
  data |> dplyr::mutate(facet = "c"),
  data |> dplyr::mutate(facet = "b"),
  data |> dplyr::mutate(facet = "a")
) |> dplyr::mutate(
  facet = factor(facet, levels = c('c', 'b', 'a'))
)

scale_params <- tibble::tibble(
  facet = c("a"),
  location = c("tl")
)

ggplot(data) + 
  geom_point(aes(x = x, y = y)) +
  annotation_scale(
    data = scale_params,
    aes(location = location)
  ) + 
  facet_wrap(~ facet)
#> Using plotunit = 'm'


ggplot(data) + 
  geom_point(aes(x = x, y = y)) +
  facet_wrap(~ facet)

Created on 2022-11-30 by the reprex package (v2.0.0)

aakarner commented 1 year ago

I just noticed this as well. It seems like it only happens when using something like scale_params to specify the facet where you'd like the scale placed.