最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

r - How to move the risktable legend text and title to the left in the plot with ggcuminc or survfit - Stack Overflow

matteradmin7PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

To 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)

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far