ggeconodist icon indicating copy to clipboard operation
ggeconodist copied to clipboard

add a legend to ggeconodist

Open smouksassi opened this issue 6 years ago • 3 comments

Hi, as discussed on twitter it might be good to have a legend to this very nice new geom. I will need some guidance to where to start but will do my best to give it a shot ! link to tweet for referece: https://twitter.com/hrbrmstr/status/1150414548740059140

smouksassi avatar Jul 15 '19 18:07 smouksassi

Welcome!

I'll start with broad pointers (below) and do not hesitate to follow-up with any questions.

https://www.hvitfeldt.me/blog/changing-glyph-in-ggplot2/ would be a good place to start to get a primer on the draw_key_* functions in {ggplot2} that work the magic for making legend glyphs.

https://github.com/tidyverse/ggplot2/blob/master/R/legend-draw.r has all the built-in draw_key_* functions.

https://www.stat.auckland.ac.nz/~paul/useR2015-grid/grid-slides.html and https://bookdown.org/rdpeng/RProgDA/the-grid-package.html are some starter resources on {grid} graphics (which are used to make the legends).

It turned out we're going to use these for a $WORK project this week so I had to toss in a bit of a kludge for one: https://github.com/hrbrmstr/ggeconodist/blob/master/R/geom-econodist.R#L1-L39

^^ is not ideal (partly b/c of time and partly b/c I had a feeling you'd take me up on the offer to help :-) but it might be useful as a building block. It may not need anything radically different, but I'll leave that up to you.

Again, do not hesitate to follow-up with any questions.

hrbrmstr avatar Jul 17 '19 10:07 hrbrmstr

Hi @hrbrmstr sorry for the very late reply was traveling and will be traveling again for most of August. do you think we also need a vertical version of the key ?

draw_key_econodistv <- function(data, params, size) {
 grobTree(
   rectGrob(
     height = 0.75, width = 0.5,
     gp = gpar(
       fill = alpha(
         data$fill %||% params$fill %||% NA,
         data$alpha %||% params$alpha %||% NA
       ),
       col = data$colour %||% params$colour %||% NA
     )
   ),
   pointsGrob(
     x = 0.5, y = 0.5, size = unit(0.25, "npc"),
     pch = data$shape,
     gp = gpar(
       col = data$median_col %||% params$median_col %||% NA
     )
   ),
   rectGrob(
     y = 0.25,
     height = 0.125, width = 0.5,
     gp = gpar(
       fill = data$tenth_col %||% params$tenth_col %||% NA,
       col = NA
     )
   ),
   rectGrob(
     y = 0.75,
     height = 0.125, width = 0.5,
     gp = gpar(
       fill = data$ninetieth_col %||% params$ninetieth_col %||% NA,
       col = NA
     )
   )
 )
 
}

smouksassi avatar Aug 07 '19 11:08 smouksassi

other things that I am trying to figure out how can we pass the size value after applying the endcap_adjust and how to make the legend work when the user changes the default legend sizes:

draw_key_econodistv <- function(data, params, size) {
  grobTree(
    rectGrob(
      width = grid::unit(0.6, "npc"),
      height = grid::unit(0.6, "npc"),
      gp = gpar(
        fill = alpha(
          data$fill %||% params$fill %||% NA,
          data$alpha %||% params$alpha %||% NA
        ),
        col = data$colour %||% params$colour %||% NA
      )
    ),
    pointsGrob(
      x = 0.5, y = 0.5, size = unit(0.25, "npc"),
      pch = data$shape,
      gp = gpar(
        col = data$median_col %||% params$median_col %||% NA,
        size = data$size %||% params$median_point_size %||% NA
      )
    ),
    rectGrob(
      y = 0.2,
      width = grid::unit(0.6, "npc"),
      height = grid::unit(0.125, "npc"),
      gp = gpar(
        fill = data$tenth_col %||% params$tenth_col %||% NA,
        col = NA,
        size = data$size %||% params$size %||% NA
        
      )
    ),
    rectGrob(
      y = 0.8,
      width = grid::unit(0.6, "npc"),
      height = grid::unit(0.125, "npc"),      gp = gpar(
        fill = data$ninetieth_col %||% params$ninetieth_col %||% NA,
        col = NA,
        size = data$size %||% params$size %||% NA
      )
    )
  )
  
}


ggplot(mammogram_costs, aes(x = state,group=city)) +
  geom_econodist(
    aes(ymin = tenth, median = median, ymax = ninetieth,fill=state),
    stat = "identity", show.legend = TRUE,median_point_size = 20,
    key_glyph=draw_key_econodistv
  ) +
  theme(legend.position="bottom")+
  theme(legend.key.height = unit(1,"cm"),legend.key.width = unit(1.5,"cm"))
  

smouksassi avatar Aug 07 '19 12:08 smouksassi