ggnewscale icon indicating copy to clipboard operation
ggnewscale copied to clipboard

Add ability to name scales

Open eliocamp opened this issue 2 years ago • 7 comments

From #48

A possible fix is to change the function to: new_scale("alpha", "some.name") instead of only adding the _new (or leaving it as the default behavior) and then using that name in the guides will probably work

eliocamp avatar Sep 01 '22 13:09 eliocamp

Hello @eliocamp,

I am sorry but I am not sure if I could not understand well what you suggested. Was it something like this? With this command I get the following error:

Error in new_scale("alpha", "Layka") : unused argument ("Layka")
  ggplot() +
  geom_raster(data = Salinity_df_r, aes(x = x, y = y, fill = Present.Surface.Salinity.Mean)) +
  scale_fill_viridis_c(option = "G", direction = -1) +
  geom_sf(data = Global, fill = "#fff7f3", colour = "#000000") +
  new_scale("alpha", "Layka") +
  geom_star(data = Coors_Grouped, aes(x = Longitude, y = Latitude, starshape = Group, fill = BioState),
            size = 6) +
  coord_sf(xlim = c(-11, 31), ylim = c(67, 49.75), expand = FALSE) +
  scale_starshape_manual(values = Shapes) +
  scale_x_continuous(breaks = seq(-10, 32, by = 10)) +
  scale_y_continuous(breaks = c(50, 58, 66)) +
  annotation_north_arrow(location = "br", which_north = "false", style = north_arrow_fancy_orienteering,
                         height = unit(2.5, "cm"), width = unit(3, "cm"),
                         pad_x = unit(2.75, "in"), pad_y = unit(.4, "in")) +
  annotation_scale(location = "br", line_width = 2, text_cex = 1.35, style = "ticks", pad_y = unit(.3, "in")) +
  theme(panel.background = element_blank(), 
        panel.border = element_rect(colour = "black", size = .5, fill = NA),
        panel.grid.major = element_line(color = "#000000", linetype = "dashed", size = .00005),
        plot.margin = margin(t = .005, b = .005, r = .2, l = .2, unit = "cm"),
        legend.background = element_rect(size = .15, color = "#000000", fill = "#fff7f3", linetype = "dotted"),
        legend.key = element_blank(),
        legend.position = c(.142, .8675),
        axis.title = element_blank(),
        axis.text = element_text(colour = "#000000", size = 15, face = "bold"),
        axis.ticks = element_line(color = "black", size = .5)) +
  guides(fill = guide_colourbar(title = "Salinity", title.theme = element_text(size = 16, face = "bold"),
                                label.theme = element_text(size = 14, face = "bold"), label.position = "top",
                                barwidth = 12.3, barheight = 1, order = 1, frame.linetype = 1, frame.colour = "#000000",
                                ticks.colour = "#ffffff", direction = "horizontal", reverse = TRUE, even.steps = TRUE,
                                draw.ulim = TRUE, draw.llim = TRUE),
         starshape = guide_legend(title = "Sampling Localities", title.theme = element_text(size = 16, face = "bold"),
                                  label.theme = element_text(size = 14),
                                  override.aes = list(starshape = Shapes, size = 4), order = 2))

Thanks in advance, George.

g-pacheco avatar Sep 12 '22 16:09 g-pacheco

Yes, this is an idea that's not implemented.

In fact, after I opened the issue I realised that is not as easy to do because new_scale() renames the previous entries.

eliocamp avatar Sep 12 '22 19:09 eliocamp

I hope you will manage to find a solution. It would be great to have this option.

g-pacheco avatar Sep 12 '22 20:09 g-pacheco

In the meantime, take a look at relayer, which already implements this but with a different syntax.

https://github.com/clauswilke/relayer

eliocamp avatar Sep 12 '22 20:09 eliocamp

@eliocamp Great work on this package, it's helping out a lot with a plotting package I'm developing that requires separation of layers.

Wanted to check in on this and see if there's any plans to address, it would be very helpful for our workflow to have a renaming function. I realize how tricky this is in the context of ggplot's layering system, unfortunately we won't be using relayer since it isn't actively maintained.

rsh52 avatar Apr 11 '24 17:04 rsh52

This is not easy/possible right now due to how ggnewscale works. It might be possible after a future rewrite which will be possible once this ggplot2 issue is resolved via this PR.

eliocamp avatar Apr 14 '24 09:04 eliocamp

Understood, I'll sub to both of those. Hopefully this is in the cards, would be incredibly useful and helpful! Thank you.

rsh52 avatar Apr 15 '24 18:04 rsh52