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

scales = 'free_y' doesn't accurately label freed y axes across rows #11

Closed jdeines closed 5 years ago

jdeines commented 6 years ago

Hi,

Thanks for the package, I've found some features really useful. I was using facet_rep_wrap() to replicate x-axis tick marks across facets, and I noticed some odd behavior when scales = 'free_y' - namely, it allows the scale of the actual plotted values to vary by panel, but it doesn't add the different y-axes across rows. In other words, it makes it look like one of the panels in each row has vastly different values, since it's axis is not re-labelled when it's scale is freed.

I'm using version 0.4.1

Here's a quick and dirty demonstration:

# format mtcars 
plotdata <- mtcars %>%
  mutate(dummy = 1:nrow(mtcars)) %>%
  select(c(dummy, mpg, cyl, disp, hp)) %>%
  tidyr::gather(., key = variable, value = value, mpg:hp)

# plotting with a typical facet is fine (scales not free)
ggplot(plotdata, aes(x = dummy, y = value)) +
  geom_line() +
  facet_rep_wrap(~variable)

# plotting with scales = 'free_y' fails to add additional y scales across row panels
ggplot(plotdata, aes(x = dummy, y = value)) +
  geom_line() +
  facet_rep_wrap(~variable, scales = 'free_y')

# behavior in base ggplot: adds y scales across rows
ggplot(plotdata, aes(x = dummy, y = value)) +
  geom_line() +
  facet_wrap(~variable, scales = 'free_y')

Cheers, Jill

stefanedwards commented 6 years ago

Hi Jill,

Thanks for your feedback! I appreciate you took the time to describe it, and with such an excellent example.

When you describe that facet_rep_wrap fails to add 'y scales', I take it you refer to the tick labels? That is at least what I see in the base ggplot example. If that is the case, you can specify which labels to add with the repeat.tick.labels argument:

ggplot(plotdata, aes(x = dummy, y = value)) +
   geom_line() +
   facet_rep_wrap(~variable, scales = 'free_y', repeat.tick.labels = 'y')

# combinations of 'x' and 'y'; does the exact same as base facet_wrap...
gplot(plotdata, aes(x = dummy, y = value)) +
    geom_line() +
    facet_rep_wrap(~variable, scales = 'free_y', repeat.tick.labels = c('x','y'))

repeat.tick.labels can also take any combination of 'left','right','top','bottom', 'x','y', or simply TRUE (for all) or FALSE (default, do not repeat the tick labels / tick text).

In my initial take, I did not consider if the labels should be repeated with free scales; in facet_grid the scales are identical across each column or row, while in wrap they can change for each panel.

I hope this satisfies your issue. If not, you are welcome to reply. :)

Cheers, Stefan

jdeines commented 6 years ago

Hi Stefan,

Thanks for the timely response! Yes, repeat.tick.labels solves the problem. But since without it, the plot is incorrect, it'd be good to adjust the default setting for future unsuspecting users in the next update.

Thanks again!

Jill