Plotly ggplot Survival Curve 如何更改图例文本
Plotly ggplot Survival Curve how to alter legend text
我正在使用 plotly 使我的生存曲线具有交互性。一切正常,除了图例以以下方式显示每个层的名称 (item_name,1)。
我想去掉括号和“,1”。通常,当我在 ggplot 中绘制图形时,它只显示:item_name
我的代码是:
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
有谁知道如何手动告诉图例文本你想要表达什么?
问题与
中的相同
类似的解决方案应该适用于您的情况:
在 p <- curve + theme(...)
应该修复它之后将其添加到您的代码中:
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
我正在使用 plotly 使我的生存曲线具有交互性。一切正常,除了图例以以下方式显示每个层的名称 (item_name,1)。
我想去掉括号和“,1”。通常,当我在 ggplot 中绘制图形时,它只显示:item_name
我的代码是:
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
有谁知道如何手动告诉图例文本你想要表达什么?
问题与
类似的解决方案应该适用于您的情况:
在 p <- curve + theme(...)
应该修复它之后将其添加到您的代码中:
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