2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ggplot2で描いた生存曲線の見栄えを微調整したい

Last updated at Posted at 2022-06-12

survminerで描画した生存曲線の微調整

Rパッケージsurvminerは生存分析と可視化が簡単にできる。
しかしggplot2は独特の癖があるため、そのままでは論文には使いづらい場合がある。
微調整の方法を忘備録として書き残す。あくまで可視化についての内容なので。

テストデータ
架空のデータ。マウスに病原体を感染させ、片方には治療介入をして生存への影響を見た。

library(survival)
library(survminer)
# テストデータ
group <- c("control", "control", "control", "control", "control", "control", "control", "control", "control", "control",
           "treated", "treated", "treated", "treated", "treated", "treated", "treated", "treated", "treated", "treated")
time <- c(25, 26, 24, 29, 28, 25, 27, 29, 30, 23, 41, 50, 44, 42, 47, 39, 49, 51, 37, 48)
censor <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
# dataframeにまとめる
data <- data.frame(group = group, time = time, censor = censor)

survdiff(Surv(time, censor) ~ group, data = data)
test_fit <- survfit(Surv(time,censor)~group, data = data)
qiita <- ggsurvplot(fit = test_fit, 
                   data = data, 
                   legend.title = "",
                   legend.labs = c("control", "treated"),
                   legend = c(0.9, 0.7),
                   font.legend = 20,
                   xlab = "days post infection", 
                   ylab = "Survival (%)",
                   font.x = 30,
                   font.y = 30,
                   xlim = c(0, 55),
                   palette = "lancet",
                   font.xtickslab = 22,
                   font.ytickslab = 22, 
                   size = 3,
                   surv.scale = c("percent")
)
qiita

c8168323-45a1-4758-a469-7af1d435e7e4.png

これでも良いが、ggplot2特有の隙間がどうしてもできてしまう。
直してみる。

qiita_new <- qiita$plot + scale_y_continuous(expand = c(0, 0), limits = c(0, 1.05), breaks = seq(0, 1, by = 0.2), labels = seq(0, 100, by = 20))
qiita_new

113c9a2f-6276-4c50-b197-eaee72edf59d.png

もう少し直す。

qiita_new + scale_x_continuous(expand = c(0, 0)) + theme(element_line(size = 2), axis.ticks = element_line(size = 2), axis.ticks.length = unit(5, "mm"))

3584405f-726e-4a36-b5f1-8f49e7b6cf66.png

だいぶそれっぽくなったのではないでしょうか。
本質ではないと言われればそれまで。でも、見栄えの良い図が欲しい。趣味みたいなものです。
おしまい。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?