R 프로그래밍 언어를 사용하고 있습니다. 이 튜토리얼을 따르려고합니다 : https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/ (페이지 하단).
이 자습서의 코드를 약간 수정하고 데이터의 3 가지 관측치에 해당하는 "계단"(예 : "생존 함수", 아래 그림에서 "빨강", "파란색", "녹색"))을 플로팅했습니다.
library(survival)
library(dplyr)
library(ranger)
library(data.table)
library(ggplot2)
library(plotly)
a = na.omit(lung)
a$ID <- seq_along(a[,1])
r_fit <- ranger(Surv(time,status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, data = a, mtry = 4,
importance = "permutation", splitrule = "extratrees", verbose = TRUE)
death_times <- r_fit$unique.death.times
surv_prob <-data.frame(r_fit$survival)
avg_prob <- sapply(surv_prob, mean)
plot(r_fit$unique.death.times, r_fit$survival[1,], type = "l", ylim = c(0,1), col = "red", xlab = "Days", ylab = "survival", main = "Survival Curves")
new = a[1:3,]
pred <- predict(r_fit, new, type = 'response')$survival
pred <- data.table(pred)
colnames(pred) <- as.character(r_fit$unique.death.times)
plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")
lines(r_fit$unique.death.times, r_fit$survival[2,], type = "l", col = "green")
lines(r_fit$unique.death.times, r_fit$survival[3,], type = "l", col = "blue")
여기에서 위의 플롯을 "대화 형"으로 만들고 싶습니다. 커브 중 하나 위로 마우스를 움직일 때 다음과 같이 만들고 싶습니다.
내 계획은 먼저 "grob"객체를 "ggplot"객체로 변환 한 다음 "ggplot"객체를 "plotly"객체로 변환하는 것이 었습니다.
grob= plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")
basic_plot = ggpubr::as_ggplot(grob)
그러나 "basic_plot"을 검사하려고하면 "NULL"로 표시됩니다.
ggplot(f)
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class gg/ggplot
이것이 효과가 있었다면 결국 ggplot 객체를 플롯으로 변환했을 것입니다.
plotly_plot = ggplotly(final_plot)
누군가이 대화 형 플롯을 만드는 방법을 보여줄 수 있습니까?
나는 이것과 비슷한 것을 얻기 위해 노력하고있다 : https://plotly.com/python/v3/ipython-notebooks/survival-analysis-r-vs-python/ (페이지 하단에 "수명"이라는 제목으로 플롯 서로 다른 종양 DNA 프로필 ")
(참고 : 저는 USB 포트 나 인터넷 연결이없는 컴퓨터로 작업하고 있습니다. 몇 개의 사전 설치된 라이브러리가있는 R 만 있습니다. "ggplotify"또는 "survminer"가 없습니다.)
감사
문제는 base
그래픽 에서 플롯을 그릴 때 장치에서 직접 그리는 것 입니다. 코드 라인은 객체를 grob= plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")
생성 NULL
합니다 ( ggplot
플롯 객체를 반환하는 것과 달리 ).
플롯을 직접 만들고 ggplot
(몇 가지 방법이 있지만 간단한 예제 bolow를 수행했습니다) 다음과 같이 변환 할 수 있습니다 ggplotly
.
p <- ggplot()+
geom_line(aes(x = r_fit$unique.death.times, y = t(pred[1,])), col = "red") +
geom_line(aes(x = r_fit$unique.death.times, y = r_fit$survival[2,]), col = "green") +
geom_line(aes(x = r_fit$unique.death.times, y = r_fit$survival[3,]), col = "blue")
ggplotly(p)
# It would be better if I put all the data into one dataframe (long data) but this will do for now!
또는 plotly
직접 플롯 할 수도 있습니다.
fig_dat <- data.frame(time = r_fit$unique.death.times,
pred_1 = t(pred[1,]),
fit_1 <- r_fit$survival[2,],
fit_2 <- r_fit$survival[3,])
fig <- plot_ly(fig_dat, x = ~time, y = ~pred_1, name = 'pred1', type = 'scatter', mode = 'lines')
fig <- fig %>% add_trace(y = ~fit_1, name = 'fit 1', mode = 'lines')
fig <- fig %>% add_trace(y = ~fit_2, name = 'fit 2', mode = 'lines')
행복한 크리스마스 :)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다