# General mapping and "lattice" ("facet" in ggplot2 nomenclature).g1 <- {ggplot(data = adlb,mapping =aes(x = AVISIT, y = AVAL, colour = SUBJID, shape = SUBJID) ) +facet_grid(LBTESTCD ~ ARM, scales ="free_y") +scale_shape_manual(values = pch)}# Add points and lines.g1 <- g1 +geom_point()g1 <- g1 +geom_line()plot <- g1plot
Experimental use!
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
The units describing rows of panes and the number of patients under each arm is specified by modifying facet_grid():
Code
# Include the units and the sample size N.g2 <- g1 +facet_grid(paste0(LBTESTCD, "\n(", AVALU, ")") ~ ARM_N,scales ="free_y")plot <- g2plot
Experimental use!
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
The functions stat_mean_ci and stat_median_ci from the tern package allow the addition of mean and/or median confidence intervals. The example below suggests a larger dataset, where the individual subject legend may not be relevant but the mean or the median are of special interest.
# General mapping and "lattice" ("facet" in ggplot2 nomenclature)g4 <- {ggplot(data = adlb,mapping =aes(x = AVISIT, y = AVAL, colour = SUBJID, shape = SUBJID) ) +facet_grid(LBTESTCD ~ ARM_N, scales ="free_y") +scale_shape_manual(values = pch) +scale_color_manual(values =rep(getOption("ggplot2.discrete.colour"), 2))}# Add points and lines.# Note that with so many patients, legend might not be useful and transparency# is advisable.g4 <- g4 +geom_point(alpha = .3)g4 <- g4 +geom_line(alpha = .3)g4 <- g4 +guides(colour ="none", shape ="none")g4 <- g4 +theme(axis.text.x =element_text(angle =45, hjust =1),axis.title =element_blank())g4 <- g4 +scale_x_continuous(breaks = adlb$AVISIT, labels = adlb$AVISIT_txt)plot <- g4plot
Experimental use!
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
# Add the mean along with the 95% CI at every visit.g51 <- g4 +stat_summary(fun = mean, linewidth =1, geom ="line",aes(group =1, linetype ="Mean +/- 95% CI"))g51 <- g51 +stat_summary(fun.data = tern::stat_mean_ci, geom ="errorbar",aes(group =1, linetype ="Mean +/- 95% CI"))plot <- g51 +guides(linetype =guide_legend(title =NULL))plot
Experimental use!
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
# Instead of a mean, the median could be more relevant.g52 <- g51 +stat_summary(fun = median, linewidth =1, geom ="line",aes(group =1, linetype ="Median +/- 95% CI"))g52 <- g52 +stat_summary(fun.data = tern::stat_median_ci, geom ="errorbar",aes(group =1, linetype ="Median +/- 95% CI"))plot <- g52 +guides(linetype =guide_legend(title ="Aggregate"))plot
Experimental use!
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
# Change the confidence level of interval for the median.# Note: check `?stat_mean_ci()` and `?stat_median_ci()` for further fine tuning.g53 <- g4 +stat_summary(fun = median, linewidth =1, geom ="line",aes(group =1, linetype ="Median +/- 80% CI"))g53 <- g53 +stat_summary(fun.data =function(x) tern::stat_median_ci(x, conf_level =0.8),geom ="errorbar", aes(group =1, linetype ="Median +/- 80% CI"))plot <- g53 +guides(linetype =guide_legend(title =NULL))plot
Experimental use!
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.