R Graphics

Insert image in R rmarkdown

![Caption for the Picture](path to the image file)
Sample Image

Sample Image

ggplot

Violin Plot

Forest Plot with Annotation

clrs <- c("#2EAEE6"
        ## , "#2EE6CA"
        ## , "#2EE677"
        , "#37E62E"
        ## , "#8AE62E"
        ## , "#DCE62E"
        ## , "#E69C2E"
        ## , "#E6492E"
          , "#aaaaaa"
          )
## clrs <- rev(clrs)

font_tick_size <- 10
font_axis_size_x <- 20
font_axis_size <- 10
font_size_annotate <- font_axis_size / .pt

dt <- data.table(
    name = (3:1)
  , name_color = clrs
  , coef_value = c(-1, -2, -3) / 10
  , coef_value_lower = (c(-1, -2, -3) - 0.5) / 10
  , coef_value_upper = (c(-1, -2, -3)+ 0.5) / 10
)

dt <- dt[, name_label := factor(paste0(name, "  ", "CI"))]
dt <- dt[, num_rep := 22 - nchar(round(coef_value, 4)) ]


tit1 <- as.expression(bquote(atop(bold('Active Arm Weighted')
                                  , ~'-0.3 (-0.25, -0.35)')))
tit2 <- as.expression(bquote(atop(bold('Active Arm')
                                  , ~'-0.2 (-0.15, -0.25)')))
tit3 <- as.expression(bquote(atop(bold('Comparator')
                                  , ~'-0.1 (-0.05, -0.15)')))



p <- ggplot(dt
          , aes(x = name
              , y = coef_value
              , ymin = coef_value_lower
              , ymax = coef_value_upper
                )) +
    geom_bar(aes(fill = name_label)
          ,  show.legend = FALSE
          , stat = "identity"
          , just = 0.5
          , size = .9) +
    geom_errorbar(aes(ymin=coef_value_lower, ymax=coef_value_upper)
                , size = 1
                , width = .05
                , color = "#333333"
                  ) +
    scale_fill_manual(values = clrs) + 
    geom_segment(aes(x = 1, xend = 3, y = -0.4, yend = -0.4)
               , color = "black"
               , size = 0.2) +
    geom_segment(aes(x = 1, xend = 1, y = -0.4, yend = -0.385)
               , color = "black"
               , size = 0.2) +
    geom_segment(aes(x = 3, xend = 3, y = -0.4, yend = -0.385)
               , color = "black"
               , size = 0.2) +
    annotate("text", x = 2, y = -0.42
           , label = "p < 0.001"
           , size = font_size_annotate
             , fontface = "italic") + 
    ## geom_segment(aes(x = 0, xend = 0, y = -0.5, yend = 0)
    ##            , color = "black"
    ##            , size = 1) +
    scale_x_continuous(expand = c(0, 0)
                     , limits = c(0.4, 4.2)
                     , breaks = c(1, 2, 3)
                     , labels = c(tit1
                                , tit2
                                , tit3)
                     , position = "top") +
    scale_y_continuous(expand = c(0, 0)
                     , limits=c(-0.5, 0)
                     , breaks = seq(-0.5, 0, .1)
                     , labels = c("-50%", "-40%", "-30%", "-20%", "-10%", "0")
                     , name='Mean') +
    ylab("") +
    xlab("") + 
    theme_bw() +
    theme(axis.ticks.x = element_blank()
        , axis.text.x = element_text(hjust = 0.5
                                   , vjust = 0
                                   , size = font_axis_size
                                   , family = "TT Arial"
                                     ## , face = "bold"
                                     )
        , axis.text.y = element_text(hjust = 0.5
                                   , vjust = 0
                                   , size = font_axis_size
                                   , family = "TT Arial")
        , axis.title.y = element_text(size = font_axis_size
                                    , family = "Times New Roman"
                                    , face = "bold")
        , panel.grid.major = element_blank()
        , panel.grid.minor = element_blank()
        , panel.border = element_blank()
        , axis.line.y = element_line(color="black", size = 0.5)
          ) +
    coord_cartesian(xlim=c(0, 4.5))
p

Arrows with Direction

Cell Plot

  • Plot discrete daily progression states

Progression Bar Plot

Animation

R sessionInfo

R version 4.2.0 (2022-04-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3 LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C

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

other attached packages: [1] gapminder_0.3.0 gifski_1.6.6-1 ggpubr_0.6.0
[4] Wu_0.0.0.9000 flexdashboard_0.6.1 lme4_1.1-31
[7] Matrix_1.5-3 mgcv_1.8-38 nlme_3.1-152
[10] png_0.1-8 scales_1.2.1 nnet_7.3-16
[13] labelled_2.10.0 kableExtra_1.3.4 plotly_4.10.1
[16] gridExtra_2.3 ggplot2_3.4.1 DT_0.27
[19] tableone_0.13.2 magrittr_2.0.3 lubridate_1.9.2
[22] dplyr_1.1.0 plyr_1.8.8 data.table_1.14.8
[25] rmdformats_1.0.4 knitr_1.42

loaded via a namespace (and not attached): [1] webshot_0.5.4 httr_1.4.5 tools_4.2.0 backports_1.4.1
[5] bslib_0.4.2 utf8_1.2.2 R6_2.5.1 DBI_1.1.3
[9] lazyeval_0.2.2 colorspace_2.1-0 withr_2.5.0 tidyselect_1.2.0 [13] compiler_4.2.0 cli_3.6.0 rvest_1.0.3 xml2_1.3.3
[17] labeling_0.4.2 bookdown_0.32 sass_0.4.5 systemfonts_1.0.4 [21] stringr_1.5.0 digest_0.6.29 minqa_1.2.5 rmarkdown_2.20
[25] svglite_2.1.1 pkgconfig_2.0.3 htmltools_0.5.4 fastmap_1.1.0
[29] highr_0.9 htmlwidgets_1.5.4 rlang_1.0.6 rstudioapi_0.14
[33] jquerylib_0.1.4 generics_0.1.3 farver_2.1.1 jsonlite_1.8.4
[37] car_3.1-1 Rcpp_1.0.10 munsell_0.5.0 fansi_1.0.4
[41] abind_1.4-5 lifecycle_1.0.3 stringi_1.7.12 yaml_2.3.7
[45] carData_3.0-5 MASS_7.3-54 grid_4.2.0 forcats_1.0.0
[49] lattice_0.20-45 cowplot_1.1.1 haven_2.5.2 splines_4.2.0
[53] hms_1.1.2 pillar_1.8.1 boot_1.3-28 ggsignif_0.6.4
[57] glue_1.6.2 evaluate_0.20 mitools_2.4 vctrs_0.5.2
[61] nloptr_2.0.3 gtable_0.3.1 purrr_1.0.1 tidyr_1.3.0
[65] cachem_1.0.6 xfun_0.37 broom_1.0.3 survey_4.1-1
[69] rstatix_0.7.2 survival_3.2-13 viridisLite_0.4.1 tibble_3.1.8
[73] timechange_0.2.0 ellipsis_0.3.2