I would like to to have the risktable title and risktable text to be positioned and aligned to the very left as shown in the first picture.
Aim of cum inc plot
I have created an object:
library(survfit)
library(tidycmprsk)
fit.mh.male <- cuminc(Surv(time, status) ~ Exposure, data = timesplit.mh.m)
fit.mh.male %>%
ggcuminc()+
add_risktable(
risktable_stats = `n.risk`,
stats_label = list(n.risk = "No. at Risk"),
size = 3)+
add_confidence_interval()+
scale_ggsurvfit()+
labs(title = "FTMH male",
x = "Time since surgery")+
theme(axis.test.y = element_text(hjust = 1))+
coord_cartesian(xlim = c(0, 10),
ylim = c(0, 0.04)) +
scale_color_jama()+
scale_fill_jama()+
theme_minimal()+
theme(legend.position = "bottom")+
guides(color = guide_legend(nrow = 1))
In the code that I have, the risktable text is is aligned to the right - and it covers some of the numbers at risk at time = 0 and the risktable title is aligned over the number at time = 0, instead of being aligned over the risk table text.
My output of the code
I would like to to have the risktable title and risktable text to be positioned and aligned to the very left as shown in the first picture.
Aim of cum inc plot
I have created an object:
library(survfit)
library(tidycmprsk)
fit.mh.male <- cuminc(Surv(time, status) ~ Exposure, data = timesplit.mh.m)
fit.mh.male %>%
ggcuminc()+
add_risktable(
risktable_stats = `n.risk`,
stats_label = list(n.risk = "No. at Risk"),
size = 3)+
add_confidence_interval()+
scale_ggsurvfit()+
labs(title = "FTMH male",
x = "Time since surgery")+
theme(axis.test.y = element_text(hjust = 1))+
coord_cartesian(xlim = c(0, 10),
ylim = c(0, 0.04)) +
scale_color_jama()+
scale_fill_jama()+
theme_minimal()+
theme(legend.position = "bottom")+
guides(color = guide_legend(nrow = 1))
In the code that I have, the risktable text is is aligned to the right - and it covers some of the numbers at risk at time = 0 and the risktable title is aligned over the number at time = 0, instead of being aligned over the risk table text.
My output of the code
Share Improve this question edited Jan 17 at 5:13 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 18, 2024 at 10:20 BiromnieBiromnie 112 bronze badges 1- @ddsjoberg maybe you know how to solve this? – Biromnie Commented Nov 18, 2024 at 10:26
1 Answer
Reset to default 0To understand the problem better, it is vital to realise that the print method for a ggcuminc
object creates two ggplot objects: the main panel at the top with the curves, and a little text-only ggplot at the bottom that acts as the risk table. The label "No. at Risk" is the title of the bottom plot, and the labels "Cataract" and "Victrectomy" are the y axis labels.
Using this information, we can pass the arguments to move these elements as a theme
object passed to the theme
argument of add_risktable
:
fit.mh.male %>%
ggcuminc() +
add_risktable(
risktable_stats = "n.risk",
stats_label = list(n.risk = " No. at Risk"),
size = 3,
theme = list(theme_risktable_default(),
theme(plot.title.position = "plot",
axis.text.y.left = element_text(hjust = 0,
margin = margin(0, 20, 0, 0))))
) +
add_confidence_interval() +
scale_ggsurvfit() +
labs(title = "FTMH male",
x = "Time since surgery")+
theme(axis.test.y = element_text(hjust = 1)) +
coord_cartesian(xlim = c(0, 10),
ylim = c(0, 0.04)) +
scale_color_jama() +
scale_fill_jama() +
theme_minimal() +
theme(legend.position = "bottom") +
guides(color = guide_legend(nrow = 1))
Data used
Note that we don't have your dataset, so I had to create one with similar characteristics and the same variable names. Here is the data I used for the above plot:
set.seed(1)
timesplit.mh.m <- data.frame(Exposure = sample(c("Cataract", "Virectomy"),
300000, TRUE, prob = c(20, 0.1)))
timesplit.mh.m$status <- rbinom(300000, size = 1,
prob = ifelse(timesplit.mh.m$Exposure == "Cataract", 0.008, 0.02))
timesplit.mh.m$time <- runif(300000, 0, 120)/10
timesplit.mh.m$status <- factor(timesplit.mh.m$status)
fit.mh.male <- cuminc(Surv(time, status) ~ Exposure, data = timesplit.mh.m)