jbengler / tidyplots

Tidy Plots for Scientific Papers
https://jbengler.github.io/tidyplots/
Other
291 stars 3 forks source link

Adjusting add_data_labels_repel aesthetics #12

Closed dtow closed 1 day ago

dtow commented 2 days ago

Hello!

Thank you for sharing this code.

I just started playing around with the plotting and I'm having trouble figuring out how to adjust the segment linetype in add_data_labels_repel. I had specified linetypes as a categorical variable in the tidyplot call, but that doesn't seem to carry over to add_data_labels_repel. When I try to specify the segment.linetype as the same categorical variable, I get an error that the categorical variable doesn't exist. Is there a trick I'm missing?

Here's some example code. The first block shows that the label linetypes aren't the same as the time series linetypes. In the second block, I try to adjust the segment.linetype but get an error.

Linetype doesn't transfer from tidy plot to add_data_labels_repel

time_course %>% tidyplot(x = day, y = score, color = treatment, dodge_width = 0, linetype = treatment ) %>% add_line( data = filter_rows( subject == 'id30' | subject == 'id22' ) ) %>% add_data_labels_repel( data = filter_rows( subject == 'id22', score == 3 ), label = subject, color = "black", background = T, na.rm = T, min.segment.length = 0, nudge_x = -5 )

add_data_labels_repel doesn't recognize the column name

time_course %>% tidyplot(x = day, y = score, color = treatment, dodge_width = 0, linetype = treatment ) %>% add_line( data = filter_rows( subject == 'id30' | subject == 'id22' ) ) %>% add_data_labels_repel( data = filter_rows( subject == 'id22', score == 3 ), label = subject, color = "black", background = T, na.rm = T, min.segment.length = 0, nudge_x = -5, segment.linetype = treatment )

Thank you for any assistance you can provide!

Cheers,

David

R version 4.4.1 (2024-06-14) Platform: x86_64-apple-darwin20 Running under: macOS 15.0.1

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles tzcode source: internal

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

other attached packages: [1] tidyplots_0.1.2 vctrs_0.6.5 robustlmm_3.3-1 foreign_0.8-87
[5] ggrepel_0.9.6 webshot_0.5.5 htmlwidgets_1.6.4 plotly_4.10.4
[9] lubridate_1.9.3 stringr_1.5.1 purrr_1.0.2 readr_2.1.5
[13] tidyr_1.3.1 tibble_3.2.1 tidyverse_2.0.0 summarytools_1.0.1 [17] gtExtras_0.5.0 gt_0.11.1 forcats_1.0.0 MetBrewer_0.2.0
[21] coin_1.4-3 survival_3.7-0 rstatix_0.7.2 car_3.1-3
[25] MASS_7.3-61 shadowtext_0.1.4 dplyr_1.1.4 xlsx_0.6.5
[29] readxl_1.4.3 DescTools_0.99.58 clubSandwich_0.5.11 effects_4.2-2
[33] carData_3.0-5 ggeffects_1.7.2 lme4_1.1-35.5 Matrix_1.7-1
[37] ggsignif_0.6.4 effsize_0.8.1 reshape2_1.4.4 RColorBrewer_1.1-3 [41] ggpubr_0.6.0 gridExtra_2.3 stargazer_5.2.3 pander_0.6.5
[45] ggplot2_3.5.1 R.matlab_3.7.0 rlang_1.1.4

loaded via a namespace (and not attached): [1] libcoin_1.0-10 jsonlite_1.8.9 rstudioapi_0.17.1 magrittr_2.0.3
[5] ggbeeswarm_0.7.2 estimability_1.5.1 TH.data_1.1-2 modeltools_0.2-23
[9] magick_2.8.5 farver_2.1.2 nloptr_2.1.1 rmarkdown_2.29
[13] ragg_1.3.3 minqa_1.2.8 paletteer_1.6.0 base64enc_0.1-3
[17] htmltools_0.5.8.1 haven_2.5.4 survey_4.4-2 broom_1.0.7
[21] cellranger_1.1.0 Formula_1.2-5 fontawesome_0.5.2 plyr_1.8.9
[25] sandwich_3.1-1 emmeans_1.10.5 rootSolve_1.8.2.4 zoo_1.8-12
[29] lifecycle_1.0.4 pkgconfig_2.0.3 R6_2.5.1 fastmap_1.2.0
[33] digest_0.6.37 Exact_3.3 colorspace_2.1-1 rematch2_2.1.2
[37] patchwork_1.3.0 textshaping_0.4.0 labeling_0.4.3 fansi_1.0.6
[41] timechange_0.3.0 httr_1.4.7 abind_1.4-8 compiler_4.4.1
[45] proxy_0.4-27 bit64_4.5.2 withr_3.0.2 backports_1.5.0
[49] DBI_1.2.3 highr_0.11 R.utils_2.12.3 fastGHQuad_1.0.1
[53] gld_2.6.6 tools_4.4.1 vipor_0.4.7 beeswarm_0.4.0
[57] nnet_7.3-19 R.oo_1.27.0 glue_1.8.0 nlme_3.1-166
[61] grid_4.4.1 checkmate_2.3.2 generics_0.1.3 gtable_0.3.6
[65] tzdb_0.4.0 R.methodsS3_1.8.2 class_7.3-22 data.table_1.16.2
[69] lmom_3.2 hms_1.1.3 xml2_1.3.6 utf8_1.2.4
[73] pillar_1.9.0 vroom_1.6.5 robustbase_0.99-4-1 mitools_2.4
[77] rJava_1.0-11 splines_4.4.1 pryr_0.1.6 lattice_0.22-6
[81] bit_4.5.0 tidyselect_1.2.1 knitr_1.49 stats4_4.4.1
[85] xfun_0.49 expm_1.0-0 rapportools_1.1 matrixStats_1.4.1
[89] DEoptimR_1.1-3 stringi_1.8.4 lazyeval_0.2.2 yaml_2.3.10
[93] boot_1.3-31 evaluate_1.0.1 codetools_0.2-20 xlsxjars_0.6.1
[97] tcltk_4.4.1 cli_3.6.3 xtable_1.8-4 systemfonts_1.1.0
[101] munsell_0.5.1 Rcpp_1.0.13-1 parallel_4.4.1 viridisLite_0.4.2
[105] mvtnorm_1.3-2 scales_1.3.0 e1071_1.7-16 crayon_1.5.3
[109] insight_0.20.5 multcomp_1.4-26

jbengler commented 2 days ago

Hi @dtow

Thank you for using tidyplots!

I am not sure if I get what you want to achieve. Here are some general thoughts:

  1. The argument segment.linetype of add_data_labels_repel() can only take static values. These can not be drawn from the dataset itself.
  2. The aesthetic linetype in tidyplot() only has an effect on lines but not on add_data_labels_repel(), which is basically a text label with a connecting line to the data point.
  3. To my knowledge there is no way to populate segment.linetype dynamically from the dataset in the sense of a ggplot2 aesthetic defined by ggplot2::aes().

Best Jan