wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
650 stars 37 forks source link

`geom_richtext` position dodge stops working when `fill` is specified #102

Closed ppm1337 closed 1 month ago

ppm1337 commented 1 year ago

Hey,

Thanks for this amazing package. I have an issue removing the fill color from the label boxes created by geom_richtext. As suggested in the documentation, this can be achieved by setting fill = NA, but doing so breaks the positioning of the labels if position dodge is specified.

library(ggplot2)
library(ggtext)

diamonds_subset <- diamonds[diamonds$cut == "Fair", ][1:10, ]

base_plot <- diamonds_subset |>
    ggplot(
        aes(
            x = cut,
            y = carat,
            fill = color
        )
    ) +
    geom_bar(
        stat = "identity",
        position = position_dodge(width = 0.9)
    )

# no fill specified: position dodge works as expected
base_plot + 
    geom_richtext(
        aes(label = clarity),
        position = position_dodge(width = 0.9)
    )

# fill is set: position dodge doesn't work 
base_plot + 
    geom_richtext(
        aes(label = clarity),
        position = position_dodge(width = 0.9),
        fill = NA, label.color = NA
    )

The reprex code produces the following two plots:

position dodge, no fill set position dodge, fill is set
image image

As you can see, the second plot does not retain the position information.

Truncated sessionInfo():

R version 4.1.3 (2022-03-10)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

[...]

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggtext_0.1.2  ggplot2_3.4.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10       xml2_1.3.3        magrittr_2.0.3    tidyselect_1.2.0  munsell_0.5.0     gridtext_0.1.5   
 [7] viridisLite_0.4.1 colorspace_2.1-0  R6_2.5.1          rlang_1.1.0       fansi_1.0.4       stringr_1.5.0    
[13] dplyr_1.1.1       tools_4.1.3       grid_4.1.3        gtable_0.3.3      xfun_0.38         utf8_1.2.3       
[19] cli_3.6.1         withr_2.5.0       commonmark_1.9.0  systemfonts_1.0.4 tibble_3.2.1      lifecycle_1.0.3  
[25] farver_2.1.1      later_1.3.0       vctrs_0.6.1       glue_1.6.2        labeling_0.4.2    stringi_1.7.12   
[31] compiler_4.1.3    pillar_1.9.0      generics_0.1.3    scales_1.2.1      httpgd_1.3.1      jsonlite_1.8.4   
[37] markdown_1.6      pkgconfig_2.0.3  
trekonom commented 1 year ago

By setting fill=NA you are dropping or removing the grouping by color for the text layer. To still dodge the labels you have to set the group aes, i..e add group=color.

library(ggplot2)
library(ggtext)

diamonds_subset <- diamonds[diamonds$cut == "Fair", ][1:10, ]

base_plot <- diamonds_subset |>
  ggplot(
    aes(
      x = cut,
      y = carat,
      fill = color
    )
  ) +
  geom_bar(
    stat = "identity",
    position = position_dodge(width = 0.9)
  )

base_plot +
  geom_richtext(
    aes(label = clarity, group = color),
    position = position_dodge(width = 0.9),
    fill = NA, label.color = NA
  )

Created on 2023-06-17 with reprex v2.0.2