我使用survival
程序包按组对生存曲线进行Kaplan-Mayer估计,然后使用ggfortify
和survminer
程序包绘制出上述曲线.除用于打印的图例标签外,所有标签均工作正常.我想在图例标签中显示N个大小的组.我认为使用paste0
将N大小添加到GROUPING变量本身是一个不错的方法.在我的情况下,它比使用像scale_fill_discrete("", labels = legend_labeller_for_plot)
这样的东西更容易.
library(dplyr)
library(ggplot2)
library(survival)
library(survminer)
library(ggfortify)
set.seed = 100
data <- data.frame(
time = rlnorm(20),
event = as.integer(runif(20) < 0.5),
group = ifelse(runif(20) > 0.5,
"group A",
"group B")
)
# Plotting survival curves without N sizes in the legend
fit <- survfit(
with(data, Surv(time, event)) ~ group,
data)
autoplot(fit)
# Adding N sizes to the data and plotting
data_new <- data %>%
group_by(group) %>% mutate(N = n()) %>%
ungroup() %>%
mutate(group_with_N = paste0(group, ", N = ", N))
fit_new <- survfit(
with(data, Surv(time, event)) ~ group_with_N,
data_new)
autoplot(fit_new)
When I try to add N sizes to the groups variable, the part with "N =" in the grouping variable disappears, i.e. the group
variable isn't displayed on the legend labels as expected.
For comparison, what I expect is something like the following using Iris data:
更重要的是,我发现罪魁祸首是等号=
.当我go 掉=
号时,图例标签对应于分组变量值.
我的问题是,为什么等号会导致这种情况?