R:绘制图形(ggplot与autoplot) [英] R: plotting graphs (ggplot vs autoplot)
问题描述
我正在这里的R教程 https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/
我用于工作的计算机没有互联网访问权限,也没有USB端口-它只有带有某些预装库的R.该教程需要生存","ggplot2",游侠","dplyr".和"ggfortify".我在工作中使用的计算机除了ggfortfiy以外,还具有所有这些库.显然地,称为自动绘图"的功能可以被称为自动绘图".必须使用ggfortify库中的内容才能制作本教程中的一些图.
The computer I use for work does not have internet access nor a USB port - it only has R with some preinstalled libraries. The tutorial requires "survival", "ggplot2", "ranger", "dplyr" and "ggfortify". The computer I use for work has all of these libraries EXCEPT ggfortfiy. Apparently, a function called "autoplot" is required from the ggfortify library to make some of the plots in this tutorial.
当我尝试运行本教程中的代码时:
When I try to run the code from the tutorial:
#load libraries
library(survival)
library(ranger)
library(ggplot2)
library(dplyr)
#load data
data(veteran)
head(veteran)
# Kaplan Meier Survival Curve
km <- with(veteran, Surv(time, status))
km_fit <- survfit(Surv(time, status) ~ 1, data=veteran)
#plot(km_fit, xlab="Days", main = 'Kaplan Meyer Plot') #base graphics is always ready
#here is where the error is
autoplot(km_fit)
我收到以下错误:错误:自动绘图不支持survfit类型的对象.
有人知道如何解决此问题吗?如果没有ggfortify库,是否可以绘制相似的图?可以用ggplot2制作吗?
Does anyone know how to fix this? Is it possible to make similar plots without the ggfortify library? Can it just be made with ggplot2?
在我的个人计算机上,安装ggfortify库后,便可以绘制该图.
On my personal computer, I am able to make this plot once I install the ggfortify library.
(注意:我也没有"survminer"库)
(note: I also do not have the "survminer" library)
谢谢
推荐答案
是的,这是可能的,因为 autoplot
函数在幕后使用了 ggplot2
:
Yes, this is possible, because the autoplot
function uses ggplot2
under the hood:
tibble(time = km_fit$time, surv = km_fit$surv,
min = km_fit$lower, max = km_fit$upper) %>%
ggplot(aes(x = time)) +
geom_line(aes(y = surv)) +
geom_ribbon(aes(ymin = min, ymax = max), alpha = 0.3)
这篇关于R:绘制图形(ggplot与autoplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!