stefanedwards / lemon

🍋 Lemon --- Freshing up your ggplots
https://cran.r-project.org/package=lemon
GNU General Public License v3.0
180 stars 11 forks source link

`reposition_legend()` places legend.box behind `facet_rep_grid()` #15

Closed dylanjm closed 4 years ago

dylanjm commented 5 years ago

Not quite sure how to make a reprex for this problem I'm having and have checked previous issues to see if it's been run into before but I'm attempting to use reposition_legend() along with facet_rep_grid().

When I do this it appears to be placing the legend behind the grid effect. Is there a way so that the legend will be placed on top? I have tried changing the fill of the legend.box just in case if it was making the box transparent but it does not look to be the case. Here is a pic to show what I mean:

image

Terribly sorry to not have a reprex to show you but here is my code just incase:

library(tidyverse)
library(lubridate)
library(lemon)

wmt <- read_csv("https://byuistats.github.io/M335/data/Walmart_store_openings.csv")

sts <- tibble(state.abb, state.region)

wmt_count <- wmt %>% 
  mutate(OPENDATE = mdy(OPENDATE),
         year = year(OPENDATE)) %>% 
  count(year, STRSTATE) %>% 
  left_join(sts, by = c("STRSTATE" = "state.abb")) %>% 
  mutate(state.region = fct_relevel(state.region, c("South", 
                                                    "North Central", 
                                                    "West", 
                                                    "Northeast")),
         STRSTATE = fct_reorder(STRSTATE, year, min, .desc = T))

p <- ggplot(wmt_count, aes(x = year, y = n, fill = state.region)) + 
  geom_bar(stat = "identity") + 
  facet_rep_grid(STRSTATE ~ .) +
  scale_x_continuous(labels = c(1962,1970,1980,1990,2000,2006),
                     breaks = c(1962,1970,1980,1990,2000,2006),
                     expand = c(0,0)) + 
  scale_y_sqrt() + 
  scale_fill_manual(values = c("#dd6d16", "#33b24e", "#fac20e", "#37aef0")) + 
  labs(title = "The Growth of Walmart: Store Openings by Year and State") + 
  theme_minimal() + 
  theme(axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.line = element_line(),
        axis.line.y = element_blank(),
        axis.line.x = element_line(linetype = "dashed",
                                   color = "lightgrey"),
        legend.title = element_blank(),
        strip.text.y = element_text(angle = 0, hjust = 0, color = "grey50", 
                                    size = 7),
        panel.grid = element_blank(),
        text = element_text(color = "grey50"), 
        legend.background = element_rect(fill = "white", color = "white"))

reposition_legend(p, "top left", panel = "panel-1-1")
stefanedwards commented 4 years ago

I am terribly sorry I haven't noticed your issue report earlier.

The legendbox is by design defaulted to be placed beneath the axis lines, as in most cases this gave the best results as it otherwise would hide some of the axis lines, when positioned in a corner or up against an axis line.

The solution to your issue is setting the z argument to reposition_legend to something large enough cover all the other elements in the plot, but nut use Inf, as this defaults it to be below the axis lines.

Ideally, this parameter should be modified to react differently towards Inf and another default value. That would however be a breaking change, so it will have to wait until a larger release. Until now, I have modified the help text to better describe how to use it.