Как я могу объединить два графика ggplot.predict в R? - PullRequest
0 голосов
/ 27 января 2019

Я создал два разных графика на основе двух разных моделей: model и model1.Пожалуйста, найдите ниже My Data.Я приложил два графика:

Модель enter image description here Модель1 enter image description here

Я хочу объединить два графика и сохранить уверенностьполосы в то же время.Я пробовал несколько решений, например, rbind, но это не похоже на работу - пожалуйста, смотрите ниже.

Я использовал следующие сценарии для создания двух графиков

model <- cph(Surv(os.neck,mors)~rcs(test),data=n)
model1 <- cph(Surv(os.neck,mors)~rcs(test),data=n1)

j <- ggplot(Predict(model, fun=exp), colfill = "blue")
k <- ggplot(Predict(model1, fun=exp), colfill = "yellow")

Я пробовал rbind:

e <- Predict(model, fun=exp, conf.int = TRUE)
f <- Predict(model1, fun=exp, conf.int = TRUE)

j <- ggplot(rbind(e,f)) 

, который дал это:

rbind()

enter image description here

My data:

n <- subset(w, w$stadie %in% 1:2)
n1 <- subset(w, w$stadie %in% 3:5)

Запрошено dput(out) из комментариев

    w <- structure(list(model = c("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"), test = c(0.0438735177865613, 0.0465676207122569, 
0.0492617236379526, 0.0519558265636483, 0.0546499294893439, 0.0573440324150396, 
0.0600381353407353, 0.062732238266431, 0.0654263411921266, 0.0681204441178223, 
0.070814547043518, 0.0735086499692136, 0.0762027528949093, 0.078896855820605, 
0.0815909587463007, 0.0842850616719963, 0.086979164597692, 0.0896732675233877, 
0.0923673704490833, 0.095061473374779, 0.05, 0.0530569514237856, 
0.0561139028475712, 0.0591708542713568, 0.0622278056951424, 0.065284757118928, 
0.0683417085427136, 0.0713986599664992, 0.0744556113902848, 0.0775125628140703, 
0.0805695142378559, 0.0836264656616415, 0.0866834170854271, 0.0897403685092127, 
0.0927973199329983, 0.0958542713567839, 0.0989112227805695, 0.101968174204355, 
0.105025125628141, 0.108082077051926), yhat = c(0.715524721809984, 
0.72420520893997, 0.732895287854242, 0.741495950465592, 0.749903690905934, 
0.758010700841758, 0.765705214141122, 0.772872009692537, 0.779393079520142, 
0.785148467039571, 0.79001727733411, 0.793878857700365, 0.796614142441177, 
0.798107151024956, 0.798246668871875, 0.796979824770716, 0.794412433838086, 
0.790683064226291, 0.785933397797749, 0.780306386213083, 1.24887346414771, 
1.12142387236568, 1.00744333341272, 0.906978784944319, 0.819807522848923, 
0.745379660125369, 0.682977886151413, 0.631846830283734, 0.591296955987878, 
0.560790614744859, 0.53975355731851, 0.52685030147002, 0.520878199524915, 
0.520957917193064, 0.526437601275528, 0.53682068603444, 0.551708849922178, 
0.570754454105439, 0.593618741429514, 0.619933518450193), lower = c(0.445870969928758, 
0.472487603995491, 0.498645159577579, 0.523317755828918, 0.545270747924011, 
0.563214260495099, 0.576107648755599, 0.583517928079882, 0.585795811114823, 
0.583918701876133, 0.579131268180072, 0.572630973080174, 0.565412209767786, 
0.558237952034289, 0.551671245622871, 0.546072898734981, 0.541548416151744, 
0.538098574671309, 0.535672640626991, 0.534183860233478, 0.613882362074539, 
0.611611984419279, 0.601234738035742, 0.579326232945668, 0.543582975437934, 
0.496000647093785, 0.443637816386947, 0.39437687025085, 0.353159479619957, 
0.321944706132161, 0.30083406381699, 0.288326373517578, 0.282948308375769, 
0.283624310505754, 0.289563062775844, 0.300128054614955, 0.314709399887597, 
0.332603569457389, 0.352917102130059, 0.374528152852913), upper = c(1.14825961332055, 
1.11002527943736, 1.07718984661152, 1.05063556210888, 1.03133268706487, 
1.02018052967182, 1.01769951541058, 1.02367230657634, 1.03697151956046, 
1.05572593121937, 1.07769573631852, 1.10061046351294, 1.12235654089946, 
1.14104571750444, 1.1550316414364, 1.16317224781343, 1.16534569433533, 
1.16183119131315, 1.15311341092747, 1.13982862772903, 2.54069024589915, 
2.05619172538896, 1.68809618910841, 1.4199434956646, 1.23639702655924, 
1.1201413566373, 1.05144055745915, 1.01230687460364, 0.990011907755607, 
0.976832690818709, 0.968420593537629, 0.962698059052612, 0.958882208194717, 
0.956889594556209, 0.957085290437296, 0.96017831230139, 0.967186411308867, 
0.979426190201882, 0.998487202942342, 1.02613799355416), .predictor. = c("test", 
"test", "test", "test", "test", "test", "test", "test", "test", 
"test", "test", "test", "test", "test", "test", "test", "test", 
"test", "test", "test", "test", "test", "test", "test", "test", 
"test", "test", "test", "test", "test", "test", "test", "test", 
"test", "test", "test", "test", "test", "test", "test"), .set. = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor")), .Names = c("model", 
"test", "yhat", "lower", "upper", ".predictor.", ".set."), row.names = c("1.1", 
"1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "1.10", 
"1.11", "1.12", "1.13", "1.14", "1.15", "1.16", "1.17", "1.18", 
"1.19", "1.20", "2.201", "2.202", "2.203", "2.204", "2.205", 
"2.206", "2.207", "2.208", "2.209", "2.210", "2.211", "2.212", 
"2.213", "2.214", "2.215", "2.216", "2.217", "2.218", "2.219", 
"2.220"), class = c("Predict", "data.frame"), info = structure(list(
    Design = structure(list(label = structure("Set", .Names = ".set."), 
        units = structure("", .Names = ".set.")), .Names = c("label", 
    "units")), varying = ".set.", adjust = structure(list(`1` = NULL, 
        `2` = NULL), .Names = c("1", "2"))), .Names = c("Design", 
"varying", "adjust")))

Спасибо заранее, Кл.

1 Ответ

0 голосов
/ 27 января 2019

Вот базовый сюжет

ggplot(as.data.frame(out), aes(x = test)) +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .3) +
  geom_line(aes(y = yhat, col = model))

Нам нужно as.data.frame(out), потому что out имеет класс Predict.

enter image description here

Вы можете добавить еще theme изменить fill и color или вы также можете добавить содержательный заголовок, подзаголовок и т. Д.В SO полно примеров.


Мы можем использовать палитру JCO из пакета ggsci

library(ggsci)
ggplot(as.data.frame(out), aes(x = test)) +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .3) +
  geom_line(aes(y = yhat, col = model)) + 
  scale_color_jco() + 
  scale_fill_jco()

Чтобы изменить метки легенды, выполните

... +
  scale_color_jco(labels = c("A", "B")) + 
  scale_fill_jco(labels = c("A", "B"))
...