Search code examples
rggplot2emmeans

Increase `emmeans` comparison arrows' thickness


I'm looking for a slick way to increase the arrows' thickness. My rough idea is with geom_line(aes(size = 5)). I did not get thicker arrows, but a new legend.

How do I change my code? Thanks a lot.

 warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
 warp.emm <- emmeans(warp.lm, ~ tension | wool)
plot(warp.emm, by = NULL, comparisons = TRUE, adjust = "mvt", 
   horizontal = FALSE, colors = c("darkgreen")) +
  geom_line(aes(size = 5))

enter image description here

This code, geom_line(arrow(size = 5)) returned an error: Error in arrow(size = 5) : unused argument (size = 5)

How do I change my code? Thanks a lot.


Solution

  • First, you a legend because you mapped on the size aes instead of using size as an argument, i.e. outside of aes(). Second, you get an error because arrow() has no size argument. See ?arrow.

    Instead you could increase the size of the arrows like so:

    library(emmeans)
    library(ggplot2)
    warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
    warp.emm <- emmeans(warp.lm, ~ tension | wool)
    g <- plot(warp.emm, by = NULL, comparisons = TRUE, adjust = "mvt", 
         horizontal = FALSE, colors = c("darkgreen"))
    
    

    Inspecting the ggplot object we see that it's composed of five layers, where the arrows are drawn via the geom_segment layers 3 and 4:

    g$layers
    #> [[1]]
    #> geom_point: na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity 
    #> 
    #> [[2]]
    #> mapping: xend = ~ucl, yend = ~pri.fac, x = ~lcl, y = ~pri.fac 
    #> geom_segment: arrow = NULL, arrow.fill = NULL, lineend = butt, linejoin = round, na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity 
    #> 
    #> [[3]]
    #> mapping: xend = ~lcmpl, yend = ~pri.fac, x = ~the.emmean, y = ~pri.fac 
    #> geom_segment: arrow = list(angle = 30, length = 0.07, ends = 2, type = 2), arrow.fill = NULL, lineend = butt, linejoin = round, na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity 
    #> 
    #> [[4]]
    #> mapping: xend = ~rcmpl, yend = ~pri.fac, x = ~the.emmean, y = ~pri.fac 
    #> geom_segment: arrow = list(angle = 30, length = 0.07, ends = 2, type = 2), arrow.fill = NULL, lineend = butt, linejoin = round, na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity 
    #> 
    #> [[5]]
    #> geom_point: na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity
    

    Hence, to increase the thickness of the arrows you can set size parameter of these layers like so:

    
    g$layers[[3]]$aes_params$size = 1.5
    g$layers[[4]]$aes_params$size = 1.5
    
    g
    

    Created on 2021-05-30 by the reprex package (v2.0.0)