library(visR) library(survival) library(ggplot2)
This tutorial illustrates the usage of the styling function that
visR provides. By default,
visR::visr() does not apply any form of visual changes to the generated survival plots. Therefore, the default output looks like you would expect from a standard
In this example, we will work with patient data from NCCTG Lung Cancer dataset that is part of the
survival package. This data is also used to demonstrate more functions of
visR in another vignette. However, in this particular one, it will only be used to demonstrate the adjustments of the aesthetics.
<- survival::lung lung_cohort <- lung_cohort %>% lung_cohort ::mutate(sex = as.factor(ifelse(sex == 1, "Male", "Female"))) %>% dplyr::mutate(status = status - 1) %>% dplyr::rename(Age = "age", Sex = "sex", Status = "status", Days = "time") dplyr <- lung_cohort %>% lung_suvival_object ::estimate_KM(strata = "Sex", CNSR = "Status", AVAL = "Days") visR
survfitobject without adjustments
<- lung_suvival_object %>% p ::visr() visRp
As we can, the plot shows the default
ggplot2::theme_grey() style with a grey background, a visible grid and the default
ggplot2to style the plot
visR::visr() also generates a valid
ggplot object as an output, we can use the conventional styling logic and options that
ggplot2 provides, as shown below.
+ p ::theme_bw() + ggplot2::theme(legend.position = "top") + ggplot2::scale_color_manual(values = c("red", "blue")) ggplot2
visR also provides functions to adjust common aesthetics more easily and with less code.
visRto style the plot
The most direct option to style plots generated through
visR::visr() is by using the parameters that the function provides. Internally, parameters like the y-axis label are automatically deducted from the used function. The following example demonstrates the options exposed.
%>% lung_suvival_object ::visr(x_label = "Time", visRy_label = NULL, # NULL (default) causes the label to be deducted from the used function x_ticks = seq(0, 1200, 200), y_ticks = seq(0, 100, 20), fun = "pct", legend_position = "top")
However, these rather minimal adjustments usually don’t cover all the things a user wants to modify. Therefore, we provide two additional functions to adjust more aesthetics:
visR::apply_theme(). The first one provides an easy wrapper to create a nested list of list with styling options that is then applied to the plot by the second function.
If no further options are provided to
visR::define_theme(), it nonetheless returns a very minimal list of reasonable defaults.
::define_theme() visR#> $fontfamily #>  "Helvetica" #> #> $grid #>  FALSE #> #> $bg #>  "transparent" #> #> attr(,"class") #>  "list" "visR_theme"
However, this function also takes several other styling options. The currently usable ones are displayed below. One particular use that we had in mind when writing this function was, that we wanted to have the option to define the different colours for the strata once and then to not have to worry about all of them being present.
<- theme ::define_theme( visRstrata = list( "Sex" = list("Female" = "red", "Male" = "blue"), "ph.ecog" = list("0" = "cyan", "1" = "purple", "2" = "brown") ),fontsizes = list( "axis" = 12, "ticks" = 10, "legend_title" = 10, "legend_text" = 8 ),fontfamily = "Helvetica", grid = list("major" = FALSE, "minor" = FALSE), #grid = TRUE/FALSE # <- can also be used instead of the named list above bg = "transparent", legend_position = "top" )
visR::apply_theme() function exposes the user to two ways to style their plot. The most direct one would be to just apply the function to a plot without specifying any options. This applies several reasonable defaults to the plot.
%>% lung_suvival_object ::visr() %>% visR::apply_theme() visR
The second one would be to apply a nested list of lists to, ideally generated through
visR::define_theme() to a plot. This serves the purpose to generate a detailed
visR_theme object once and then apply it to one or several plots with a single line. These lists could then also be easily saved and shared. The usage of the theme generated above is shown below.
%>% lung_suvival_object ::visr() %>% visR::apply_theme(theme) visR
Applying a theme does not prevent the user from further applying modifications, as for example the addition of confidence intervals, censoring indicators or risk tables.
%>% lung_suvival_object ::visr() %>% visR::apply_theme(theme) %>% visR::add_CI() %>% visR::add_CNSR() %>% visR::add_risktable() visR