I am using plotly to make my survival curves interactive. All is working well except the legend is displaying the name of each strata in the following fashion (item_name,1).
I want to get rid of the brackets and the ",1". Normally when I plot the figures in ggplot it shows just: item_name
My code is:
km_curve <- ggsurvplot(fit,
data = dataset,
censor = FALSE,
conf.int = TRUE,
risk.table = TRUE,
xlab = "Time since Hospital Discharge (years)",
ylab = "Proportion of patients alive",
palette = c("red","orange","yellow","green","blue","brown","pink","black"),
legend.title = "Diagnosis",
legend.labs = c("Cardiac (Non-surgical)","Cardiac (Surgical)","Gastrointestinal","Neurological","Other","Respiratory","Sepsis","Trauma"))
curve <- km_curve$plot + scale_y_continuous(breaks = seq(0, 1, .25), labels = scales::percent) +
scale_x_continuous( breaks = seq(0,10,1), expand = c(0, 0))
p <- curve + theme(legend.title = element_text(size = 8), legend.text = element_text(size =
8),legend.position = "bottom") + coord_cartesian(xlim = c(0,10))
g <- ggplotly(p)
g
Does anyone know how to manually tell plotly what you want the legend text to say?
The issue is the same as in Strange formatting of legend in ggplotly in R
A similar solution should apply in your case:
Add this to your code after p <- curve + theme(...)
should fix it:
p <- ggplotly(p)
for (i in 1:length(p$x$data)){
if (!is.null(p$x$data[[i]]$name)){
p$x$data[[i]]$name = gsub("\\(","",strsplit(p$x$data[[i]]$name,",")[[1]][1])
}
}
p