我如何在R / ggplot中切割图形?

时间:2017-11-11 20:55:27

标签: r ggplot2 graph

在R;

我有以下代码:

ggsurvplot(curve.histology, conf.int=TRUE, 
legend.labs=legend.histology, surv.scale=c("percent"),risk.table=T, 
ggtheme = theme_bw(), risk.table.fontsize=3.5, xlim=c(0,5), 
ylim=c(0,1), break.time.by=1)

“curve.histology”中有四个变量。我希望所有四个图形在x = 5处停止,就像我的x轴一样。任何人都可以帮忙吗?

See graph here

curve.histology <- 
rs.surv(Surv(time,cens)~ordered(histologi)+ratetable(age = age, sex = 
sex, year = year), ratetable = poptab, method="ederer2")

提前致谢,

℃。

1 个答案:

答案 0 :(得分:0)

IIUC,在xlim上设置expand = 0Lcoord_cartesian()可能会有所帮助(只要按theme()排列边距)。但是,我认为你应该向survminer包的存储库提交一个问题,因为在ggsurvplot中编辑ggplot对象有点痛苦......

reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-11-12

library(survminer)
#> Loading required package: ggplot2
#> Loading required package: ggpubr
#> Loading required package: magrittr
library(survival)

fit <- survfit(Surv(time, status) ~ sex, data = lung)
p <- ggsurvplot(fit, data = lung, xlim = c(0, 900), expand = FALSE)

p$plot <- p$plot +
  coord_cartesian(xlim = c(0, 900), expand = FALSE) +
  theme(plot.margin = margin(5.5, 20, 5.5, 5.5))
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
p

example plot