我正在尝试绘制累积危害函数,并从威布尔分布看它如何与累积危害函数相适应。
我通过以下方式进行操作:
library(ggplot2)
df1 <- structure(list(time = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), cumhaz = c(
0.0012987012987013,
0.00259909141573641, 0.00390287238053432, 0.00521006192301798,
0.00652412236979854, 0.00916613029582231, 0.0158150664660351,
0.0184996302244243, 0.019847339119303, 0.0225647304236509
)), row.names = c(
NA,
-10L
), class = "data.frame")
df2 <- structure(list(
time = c(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
), variable = c(
"est",
"est", "est", "est", "est", "est", "est", "est", "est", "est",
"lcl", "lcl", "lcl", "lcl", "lcl", "lcl", "lcl", "lcl", "lcl",
"lcl", "ucl", "ucl", "ucl", "ucl", "ucl", "ucl", "ucl", "ucl",
"ucl", "ucl"
), value = c(
0.000427087666907353, 0.00125579463203928,
0.00236002165980674, 0.00369249753634486, 0.00522530742007584,
0.0069393306375308, 0.00882036474985765, 0.010857299201679, 0.0130411216860664,
0.0153643233399894, 0.000132730646554643, 0.00047934870180344,
0.00101968629026559, 0.00175966372305041, 0.00268278540068723,
0.00376105113380831, 0.00500451835889961, 0.00634199908365718,
0.0079694006901553, 0.00969173651303582, 0.00113691538883517,
0.00283227928015465, 0.00480474736249053, 0.00698677110701666,
0.00936461891781656, 0.0119398743413438, 0.0146624641508895,
0.0175805674751187, 0.0205366926372124, 0.0235955294708706
),
type = c(
"Estimate", "Estimate", "Estimate", "Estimate",
"Estimate", "Estimate", "Estimate", "Estimate", "Estimate",
"Estimate", "Confidence Interval", "Confidence Interval",
"Confidence Interval", "Confidence Interval", "Confidence Interval",
"Confidence Interval", "Confidence Interval", "Confidence Interval",
"Confidence Interval", "Confidence Interval", "Confidence Interval",
"Confidence Interval", "Confidence Interval", "Confidence Interval",
"Confidence Interval", "Confidence Interval", "Confidence Interval",
"Confidence Interval", "Confidence Interval", "Confidence Interval"
)
), row.names = c(NA, -30L), class = "data.frame")
ggplot() +
geom_step(ggplot2::aes(x = time, y = cumhaz), df1, group = 1, colour = "#4C5D8A") +
geom_line(ggplot2::aes(x = time, y = value, group = variable, linetype = type), df2,
colour = "#F3C911", show.legend = FALSE) +
theme_minimal() +
labs(
x = "Time", y = "Cumulative Hazard",
title = "Weibull Distribution"
) +
scale_x_continuous(limits = c(0, 10), breaks = seq(0, 10, by = 2)) +
theme(
legend.position = "bottom",
legend.direction = "horizontal",
plot.title = element_text(hjust = 0.5)
) +
scale_linetype_manual(values = c(2, 1))
基本上,我想组合颜色和线型比例,但并非所有组合都应显示在图例中(没有紫色的置信度限制图例键)
答案 0 :(得分:1)
我的解决方案是打电话给暨。危险颜色在geom_step
中是一种美学。然后显示线型的图例,并调整标题和图例顺序。
ggplot() +
geom_step(ggplot2::aes(x = time, y = cumhaz, colour = "Actual Cumulative Hazard"), df1, group = 1) +
geom_line(ggplot2::aes(x = time, y = value, group = variable, linetype = fct_rev(type)), df2,
colour = "#F3C911") +
theme_minimal() +
scale_colour_manual(values = "#4C5D8A",
name = '',
guide = guide_legend(order = 1)) +
scale_linetype_manual(values = c(1, 2),
labels = c('Weibull Cumulative Hazard','95% Confidence Limits'),
guide = guide_legend(title = NULL, order = 0)) +
labs(
x = "Time", y = "Cumulative Hazard",
title = "Weibull Distribution"
) +
scale_x_continuous(limits = c(0, 48), breaks = seq(0, 48, by = 12)) +
theme(
legend.position = "bottom",
legend.direction = "horizontal",
plot.title = element_text(hjust = 0.5)
)
答案 1 :(得分:0)
这是一个不完善的解决方案:
df1$group <- "a"
ggplot() +
geom_step(aes(x = time, y = cumhaz, color = group),
df1,
group = 1) +
geom_line(aes(x = time, y = value, group = variable, linetype = type),
df2,
colour = "#F3C911") +
theme_minimal() +
labs(x = "Time",
y = "Cumulative Hazard",
title = "Weibull Distribution") +
scale_x_continuous(limits = c(0, 10),
breaks = seq(0, 10, by = 2)) +
theme(legend.position = "bottom",
legend.direction = "horizontal",
plot.title = element_text(hjust = 0.5)) +
scale_linetype_manual(values = c(2, 1),
name = "Line",
labels = c("Actual Cumulative Hazard",
"Weibull Cumulative Hazard",
"95% Confidence Interval")) +
scale_color_manual(values = "#4C5D8A",
name = "",
labels = c("Actual Cumulative Hazard",
"Weibull Cumulative Hazard",
"95% Confidence Interval"))
首先,需要在美学范围内调用geom_step
的颜色,因此我为此创建了一个恒定值。然后,我稍后指定一个手动色标,该色标传递该行所需的颜色值。这两个手动秤的标签相同。
这个答案不完美的地方是名称不能设置相同(如this答案),因此图例的两个部分之间有多余的空间。这里可能有解决方法,但是我尝试为两条线都定义linetype
和color
的任何替代方法最终都会产生color和linetype值的所有可能组合,这在这里是没有意义的。 / p>