Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to customize plot compnents in NIH Joinpoint package in R #9

Open
mulugeta2000 opened this issue May 31, 2024 · 1 comment
Open
Labels
bug Something isn't working reprex Needs a reproducible example

Comments

@mulugeta2000
Copy link

mulugeta2000 commented May 31, 2024

Hi Dan,
I have been using the National Institutes of Health's (NIH) Joinpoint Regression Software (version 4.8.0.1) to analyse the trend of cancer by sex. Then, I utilised the nih.joinpoint R package to plot a regression output. Specifically, I used the jp_plot function with default parameters, which resulted in a plot displaying the trends for "Both sexes", "Females", and "Males". I attempted to customise the plots by modifying labels, colours, and axis ranges using the themes function from R. However, I encountered an issue where the customisation only applied to specific plots (e.g., only the male plot). I then tried using the patchwork package with themes, but this did not produce the desired customised plots either. As a third approach, I split the data into three subsets - "Both_sex", "Male_sex", and "Female_sex" - and applied the code below to each of these datasets separately.
#================================================
run_opt = run_options(model="ln", max_joinpoints=3, n_cores=3)
export_opt = export_options()

JP_male = joinpoint(Male_sex, x=year, y=asr, se=se,
run_opts=run_opt, export_opts=export_opt)
jp_plot(JP_male)
#=================================================
This worked only for the "Both_sex" dataset. Applying the same function to the other "Male_sex" and "Female_sex" datasets produces an error message described below.
Error in mutate( ): ℹ In argument: slope0 = na_if(apc, ".") %>% zoo::na.locf(fromLast = TRUE) %>% as_factor(). Caused by error in na_if(): ! Can't convert y<character> to match type ofx<double>. Runrlang::last_trace()` to see where the error occurred.

Q1. Is there any parameter to work around inside the jp_function ( ) to customise the plot to my requirements, such as background colour and axis title and label?

Q2. Why was the dependent variable considered a character in the error message while it is numeric in structure, as shown below?

**

data structre

str(Female_sex) tibble [28 × 4] (S3: tbl_df/tbl/data.frame) $ sex : chr [1:28] "Female" "Female" "Female" "Female" ... $ year: num [1:28] 1990 1991 1992 1993 1994 ... $ asr : num [1:28] 416 396 620 520 649 ... $ se : num [1:28] 69.6 64.4 83.1 76.8 83.3 ... > str(Male_sex) tibble [28 × 4] (S3: tbl_df/tbl/data.frame) $ sex : chr [1:28] "Male" "Male" "Male" "Male" ... $ year: num [1:28] 1990 1991 1992 1993 1994 ... $ asr : num [1:28] 780 569 709 678 684 ... $ se : num [1:28] 89.6 67.7 79.9 76.4 78.6 ...
**Session info**
``` r sessionInfo() R version 4.4.0 (2024-04-24 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.utf8 LC_CTYPE=English_Australia.utf8 LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Australia.utf8

time zone: Australia/Adelaide
tzcode source: internal

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

other attached packages:
[1] patchwork_1.2.0 readxl_1.4.3 nih.joinpoint_0.1.0.9003 lubridate_1.9.3 forcats_1.0.0
[6] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1
[11] tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0

loaded via a namespace (and not attached):
[1] gtable_0.3.5 htmlwidgets_1.6.4 devtools_2.4.5 remotes_2.5.0 lattice_0.22-6 tzdb_0.4.0 vctrs_0.6.5
[8] tools_4.4.0 generics_0.1.3 parallel_4.4.0 fansi_1.0.6 pkgconfig_2.0.3 lifecycle_1.0.4 farver_2.1.2
[15] compiler_4.4.0 munsell_0.5.1 janitor_2.2.0 snakecase_0.11.1 httpuv_1.6.15 htmltools_0.5.8.1 usethis_2.2.3
[22] crayon_1.5.2 later_1.3.2 pillar_1.9.0 urlchecker_1.0.1 ellipsis_0.3.2 cachem_1.1.0 sessioninfo_1.2.2
[29] mime_0.12 tidyselect_1.2.1 digest_0.6.35 stringi_1.8.4 labeling_0.4.3 fastmap_1.2.0 grid_4.4.0
[36] colorspace_2.1-0 cli_3.6.2 magrittr_2.0.3 pkgbuild_1.4.4 utf8_1.2.4 withr_3.0.0 scales_1.3.0
[43] promises_1.3.0 bit64_4.0.5 timechange_0.3.0 bit_4.0.5 cellranger_1.1.0 zoo_1.8-12 hms_1.1.3
[50] memoise_2.0.1 shiny_1.8.1.1 miniUI_0.1.1.1 profvis_0.3.8 rlang_1.1.3 Rcpp_1.0.12 xtable_1.8-4
[57] glue_1.7.0 pkgload_1.3.4 rstudioapi_0.16.0 vroom_1.6.5 R6_2.5.1 fs_1.6.4

</details>
sex    year   asr    se
   <chr> <dbl> <dbl> <dbl>
 1 Both   1990  603.  56.8
 2 Both   1991  498.  47.5
 3 Both   1992  671.  57.6
 4 Both   1993  614.  54.8
 5 Both   1994  669.  57.0
 6 Both   1995  700.  58.4
 7 Both   1996  562.  49.5
 8 Both   1997  722.  58.2
 9 Both   1998  562.  50.5
10 Both   1999  654.  54.2
11 Both   2000  623.  52.2
12 Both   2001  628.  53.3
13 Both   2002  591.  51.0
14 Both   2003  660.  54.5
15 Both   2004  689.  57.3

head(Female_sex,15)
# A tibble: 15 × 4
   sex     year   asr    se
   <chr>  <dbl> <dbl> <dbl>
 1 Female  1990  416.  69.6
 2 Female  1991  396.  64.4
 3 Female  1992  620.  83.1
 4 Female  1993  520.  76.8
 5 Female  1994  649.  83.3
 6 Female  1995  541.  76.8
 7 Female  1996  457.  66.9
 8 Female  1997  680.  85.9
 9 Female  1998  437.  71.1
10 Female  1999  550.  75.1
11 Female  2000  550.  74.4
12 Female  2001  539.  74.9
13 Female  2002  538.  73.3
14 Female  2003  589.  78.9
15 Female  2004  535.  79.1

head(Male_sex,15)
# A tibble: 15 × 4
   sex    year   asr    se
   <chr> <dbl> <dbl> <dbl>
 1 Male   1990  780.  89.6
 2 Male   1991  569.  67.7
 3 Male   1992  709.  79.9
 4 Male   1993  678.  76.4
 5 Male   1994  684.  78.6
 6 Male   1995  823.  85.5
 7 Male   1996  652.  72.6
 8 Male   1997  749.  79.0
 9 Male   1998  617.  67.0
10 Male   1999  735.  77.1
11 Male   2000  671.  72.1
12 Male   2001  690.  74.6
13 Male   2002  631.  70.6
14 Male   2003  702.  74.1
15 Male   2004  773.  78.2
@mulugeta2000 mulugeta2000 added the bug Something isn't working label May 31, 2024
@mulugeta2000 mulugeta2000 changed the title how custmise each How to customize plot compnents in NIH Joinpoint package in R May 31, 2024
@DanChaltiel
Copy link
Owner

Hi Mulugeta,

It is difficult to see the issue without a reproducible example.
If you don't know how, please read this: https://r4ds.hadley.nz/workflow-help.html#making-a-reprex.

About your Q1, as stated in the documentation, jp_plot() returns either a patchwork or a list of ggplots. See the documentation of patchwork on how to change the background.

About your Q2, I don't know why you get this behavior. If you add a reprex I will probably be able to help, and correct the bug if there is one.

@DanChaltiel DanChaltiel added the reprex Needs a reproducible example label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working reprex Needs a reproducible example
Projects
None yet
Development

No branches or pull requests

2 participants