I have this survival dataframe:



df <- structure(list(time = c(5, 6, 8, 10, 11, 12, 13, 14, 15, 17, 
18, 20, 22, 27, 31, 32, 34, 39, 43, 44, 47, 48, 50, 60, 69, 77, 
120, 1, 4, 6, 7, 8, 9, 11, 12, 13, 14, 15, 17, 19, 22, 23, 25, 
26, 27, 31, 38, 40, 64, 89, 96, 105, 117), surv = c(0.96969696969697, 
0.93841642228739, 0.906057235311963, 0.906057235311963, 0.871208880107657, 
0.871208880107657, 0.871208880107657, 0.871208880107657, 0.829722742959673, 
0.786053124909164, 0.742383506858655, 0.695984537679989, 0.649585568501323, 
0.603186599322657, 0.603186599322657, 0.603186599322657, 0.603186599322657, 
0.542867939390391, 0.482549279458125, 0.42223061952586, 0.42223061952586, 
0.351858849604883, 0.351858849604883, 0.351858849604883, 0.351858849604883, 
0.175929424802442, 0.175929424802442, 1, 0.96551724137931, 0.96551724137931, 
0.929757343550447, 0.892567049808429, 0.892567049808429, 0.892567049808429, 
0.850063856960409, 0.805323653962492, 0.760583450964576, 0.760583450964576, 
0.760583450964576, 0.706256061609964, 0.651928672255351, 0.597601282900738, 
0.597601282900738, 0.597601282900738, 0.597601282900738, 0.522901122538146, 
0.522901122538146, 0.435750935448455, 0.435750935448455, 0.435750935448455, 
0.435750935448455, 0.435750935448455, 0.435750935448455), V3 = c(0.97, 
0.94, 0.91, 0.91, 0.87, 0.87, 0.87, 0.87, 0.83, 0.79, 0.74, 0.7, 
0.65, 0.6, 0.6, 0.6, 0.6, 0.54, 0.48, 0.42, 0.42, 0.35, 0.35, 
0.35, 0.35, 0.18, 0.18, 1, 0.97, 0.97, 0.93, 0.89, 0.89, 0.89, 
0.85, 0.81, 0.76, 0.76, 0.76, 0.71, 0.65, 0.6, 0.6, 0.6, 0.6, 
0.52, 0.52, 0.44, 0.44, 0.44, 0.44, 0.44, 0.44), grupo = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2), risk = c(33, 31, 29, 27, 26, 24, 
23, 22, 21, 19, 18, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 
4, 3, 2, 1, 30, 29, 28, 27, 25, 24, 23, 21, 19, 18, 16, 15, 14, 
13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)), class = "data.frame", row.names = c(NA, 
-53L))

With this code I constructed an animated Kaplan Meier plot:

library(ggplot2)
library(gganimate)

ggplot(data=df, aes(x=time, y=surv, group=grupo, color=as.factor(grupo))) +
  geom_line(size=1.5) +
  geom_point(size = 2, color="gray")+
  guides(color = guide_legend(
    override.aes=list(shape = 1)))+
  scale_color_brewer(name="Gender",palette="Set1",labels=c("Female","Male"))+
  labs(title = "Survival according to gender", x = "Time [weeks]", y = "Survival [%]")  +
  scale_y_continuous(limits = c(0,1))+
  geom_text(mapping=aes(y=V3,label=format(V3)),size=6,hjust=-0.2,vjust=0.5, show.legend = FALSE)+
  transition_reveal(time)+
  theme_minimal(base_size = 16) +
  theme(axis.line = element_line(colour = "black", size = 0.24),
        aspect.ratio=4/5)

Which looks like this: enter image description here

However at the end x=125 the numbers disappear. I think the grid is to short.

How can I expand the grid to avoid hiding the survival curve numbers at x = 125.

我try 过一些方法,比如扩展x,但无法解决它!我还更改了hjust和vjust but I want to keep the numbers at this position!

推荐答案

除了关闭剪裁,还可以使用expansion扩展x轴的右上限.

ggplot(df, aes(x = time, y = surv, group = grupo, color = as.factor(grupo))) +
    geom_line(size = 1.5) +
    geom_point(size = 2, color = "gray") +
    guides(color = guide_legend(override.aes = list(shape = 1))) +
    scale_color_brewer(
        name = "Gender",palette = "Set1", labels = c("Female", "Male")) +
    labs(
        title = "Survival according to gender", 
        x = "Time [weeks]", 
        y = "Survival [%]")  +
    scale_y_continuous(limits = c(0, 1)) +
    scale_x_continuous(                                    # This is new
        breaks = scales::breaks_width(25),                 # Label every 25 weeks
        expand = expansion(mult = c(0, 0.15))) +           # 15% increase on the right
    geom_text(
        aes(y = V3, label = format(V3)),
        size = 6,
        hjust = -0.2,
        vjust = 0.5, 
        show.legend = FALSE) +
    transition_reveal(time) +
    theme_minimal(base_size = 16) +
    theme(
        axis.line = element_line(colour = "black", size = 0.24),
        aspect.ratio = 4/5)

这会将右侧上限增加15%.

enter image description here

R相关问答推荐

如何从其他前面列中减go 特定列的平均值?

R中具有gggplot 2的Likert图,具有不同的排名水平和显示百分比

R:连接值,而不是变量?

pickerInput用于显示一条或多条geom_hline,这些线在图中具有不同 colored颜色

IMF IFS数据以R表示

根据日期从参考帧中创建不同的帧

从所有项的 struct 相同的两级列表中,将该第二级中的所有同名项绑定在一起

计算两列中满足特定条件连续行之间的平均值

用两种 colored颜色 填充方框图

用R ggplot2求上、下三角形中两个变量的矩阵热图

您是否可以将组添加到堆叠的柱状图

如何在使用箭头R包(箭头::OPEN_DATASSET)和dplyr谓词时编写具有整齐计算的函数?

使用ggplot2中的sec_axis()调整次轴

按组使用dummy r获取高于标准的行的平均值

以R表示的NaN值的IS.NA状态

组合名称具有模式的列表的元素

如何使用list_rind在列表中保留已命名但不包含第0行的记录?

Package emMeans:如果emmip模型中包含的变量较少,emMeans模型中的其他变量设置为什么?

基于已有ID列创建唯一ID

使用相对风险回归计算RR