为什么ggplot2会在随机点削减x值?

时间:2019-06-27 08:47:16

标签: r ggplot2

请在下面找到我的数据示例t

我有以下暨。剧情图

enter image description here

如您所见,情节自动在时间= 72处停止,我没有得到。我希望图表继续上升到84。

我使用了以下脚本

library(ggplot2)
ggplot(res, aes(time, P)) + theme +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill = CIF), alpha = 0.2) +
  geom_line(aes(color = CIF)) +
  scale_fill_manual(values = c("#F0746B", "#51BFC4"), name="", label=c("Recurrent","Non-recurrent")) +
  scale_color_manual(values = c("#F0746B", "#51BFC4"), name="",label=c("Recurrent","Non-recurrent")) +
  scale_x_continuous(name="Time (months)", breaks=seq(0,84,12), limits=c(0,90)) +
  scale_y_continuous(name="Cumulative incidence", breaks=seq(0,1,0.1), limits=c(0,1), label=c("0%","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"))

如何更改截止值?

   res <- structure(list(CIF = c("0 1", "0 1", "0 1", "0 1", "0 1", "0 1", 
"0 1", "0 1", "0 1", "0 1", "0 1", "0 1", "0 1", "0 1", "0 1", 
"0 1", "0 1", "0 1", "0 1", "0 2", "0 2", "0 2", "0 2", "0 2", 
"0 2", "0 2", "0 2", "0 2", "0 2", "0 2", "0 2", "0 2", "0 2", 
"0 2", "0 2", "0 2", "0 2", "0 2"), P = c(0, 0, 0, 0.0228748409933079, 
0.0463362163710596, 0.070431682975237, 0.0959445299678954, 0.122254653429074, 
0.122254653429074, 0.150379268163438, 0.180753852076551, 0.211128435989664, 
0.211128435989664, 0.250168710019165, 0.298218278055474, 0.350635988640538, 
0.350635988640538, 0.350635988640538, 0.350635988640538, 0.0204081632653061, 
0.0408163265306122, 0.0621315192743764, 0.0621315192743764, 0.0621315192743764, 
0.0621315192743764, 0.0621315192743764, 0.0621315192743764, 0.0902561340087402, 
0.0902561340087402, 0.0902561340087402, 0.0902561340087402, 0.12518690550882, 
0.12518690550882, 0.12518690550882, 0.12518690550882, 0.230022326678948, 
0.334857747849077, 0.649364011359462), time = c(0.72, 1, 1.34, 
3.24, 4, 7, 8.4, 10, 11.08, 12, 15, 16, 21.37, 24, 29, 38, 62.3, 
73.5, 91, 0.72, 1, 1.34, 3.24, 4, 7, 8.4, 10, 11.08, 12, 15, 
16, 21.37, 24, 29, 38, 62.3, 73.5, 91), var = c(0, 0, 0, 0.000511214638118017, 
0.00102392362117005, 0.00153824582995482, 0.00208707748578502, 
0.00263792676637738, 0.00263792676637738, 0.0032347785651533, 
0.00389314655684779, 0.00449221319277437, 0.00449221319277437, 
0.00549403125440833, 0.00693894516909676, 0.00841121294538065, 
0.00841121294538065, 0.00841121294538065, 0.00841121294538065, 
0.000407993268111076, 0.000798986816717524, 0.00120811190703145, 
0.00120811190703145, 0.00120811190703145, 0.00120811190703145, 
0.00120811190703145, 0.00120811190703145, 0.00190357162714157, 
0.00190357162714157, 0.00190357162714157, 0.00190357162714157, 
0.00292924470408858, 0.00292924470408858, 0.00292924470408858, 
0.00292924470408858, 0.0113606821657151, 0.0161245977206576, 
0.00841121294538066), lower = c(0, 0, 0, 0.00325466937258645, 
0.0117928704453157, 0.0232644190108184, 0.0370774795894332, 0.0526759464740041, 
0.0526759464740041, 0.0702619812851594, 0.0899867619108828, 0.110868524682253, 
0.110868524682253, 0.136612500412096, 0.167726987920347, 0.203404312528813, 
0.203404312528813, 0.203404312528813, 0.203404312528813, 0.00290019029599575, 
0.0103670858833598, 0.0204596376632281, 0.0204596376632281, 0.0204596376632281, 
0.0204596376632281, 0.0204596376632281, 0.0204596376632281, 0.0344121585374189, 
0.0344121585374189, 0.0344121585374189, 0.0344121585374189, 0.0525840116310767, 
0.0525840116310767, 0.0525840116310767, 0.0525840116310767, 0.0884240190052584, 
0.150306335704256, 0.474061916080192), upper = c(0, 0, 0, 0.151478684643073, 
0.172830345261227, 0.202763794202745, 0.236063283127377, 0.269643284651086, 
0.269643284651086, 0.305484320052658, 0.343957865412452, 0.380352819307292, 
0.380352819307292, 0.431239904792067, 0.494941481292974, 0.559459308224091, 
0.559459308224091, 0.559459308224091, 0.559459308224091, 0.136173097128375, 
0.153497742263838, 0.180488032326637, 0.180488032326637, 0.180488032326637, 
0.180488032326637, 0.180488032326637, 0.180488032326637, 0.225482610648669, 
0.225482610648669, 0.225482610648669, 0.225482610648669, 0.281902515392239, 
0.281902515392239, 0.281902515392239, 0.281902515392239, 0.521943368388326, 
0.639685296961992, 0.818999110910571), n.risk = c(49, 48, 45, 
41, 39, 37, 34, 32, 29, 28, 25, 24, 20, 17, 13, 11, 5, 4, 1, 
49, 48, 45, 41, 39, 37, 34, 32, 29, 28, 25, 24, 20, 17, 13, 11, 
5, 4, 1), n.event = c(0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 
0, 1, 1, 1)), row.names = c(NA, -38L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x7fadc40080e0>)

1 个答案:

答案 0 :(得分:1)

正如PoGibas所说,问题是limit中的scale_x_continuous参数,如果要将x轴的范围保持在0到90之间,可以使用coord_cartesian并删除{{来自limit new_graph

的1}}自变量
scale_x_continuous