Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ggsurvplot“分面返回:”;grDevices::col2rgb(颜色,TRUE)中出错:颜色名称无效;在函数中使用时_R_Ggplot2_Facet_Survival Analysis_Color Palette - Fatal编程技术网

ggsurvplot“分面返回:”;grDevices::col2rgb(颜色,TRUE)中出错:颜色名称无效;在函数中使用时

ggsurvplot“分面返回:”;grDevices::col2rgb(颜色,TRUE)中出错:颜色名称无效;在函数中使用时,r,ggplot2,facet,survival-analysis,color-palette,R,Ggplot2,Facet,Survival Analysis,Color Palette,我试图用ggsurvplot_facet()函数通过变量sex绘制几个变量facet的生存曲线。当我将我的代码应用到一个单独安装的模型时,它工作得很好。但是,当我尝试在函数或for循环中使用相同的代码时,它无法绘制所有应该绘制的生存曲线,并返回错误。如果允许输入一个survfit元素列表,我会在ggsurvplot_facet()本身中执行此打印,就像ggsurvplot()一样,但是ggsurvplot_facet()一次只允许一个survfit元素 我正在使用Mac OS High Sier

我试图用ggsurvplot_facet()函数通过变量sex绘制几个变量facet的生存曲线。当我将我的代码应用到一个单独安装的模型时,它工作得很好。但是,当我尝试在函数或for循环中使用相同的代码时,它无法绘制所有应该绘制的生存曲线,并返回错误。如果允许输入一个survfit元素列表,我会在ggsurvplot_facet()本身中执行此打印,就像ggsurvplot()一样,但是ggsurvplot_facet()一次只允许一个survfit元素

我正在使用Mac OS High Sierra在2018年的MacBook Pro中运行我的代码

考虑以下数据集:

它包含对100名受试者和4个不同变量的多次访问的观察结果。其中两个变量(variable1和variable2)可以有两个不同的值(0或1),另外两个变量(variable3和variable4)可以有三个不同的值(0、1或2)

我已经开始使用具有两个不同值的函数,并编写了以下代码:

# Load libraries
require(mgcv)
require(msm)
library(dplyr)
library(grDevices)
library(survival)
library(survminer)


# Set working directory
dirname<-dirname(rstudioapi::getSourceEditorContext()$path)
setwd(dirname)


load("ggsurvplot_facet_error.rda")


fit_test <- survfit(
  Surv(follow_up, as.numeric(status)) ~ (sex + variable1), data = data)

plot_test <- ggsurvplot_facet(fit_test,
                                     data = data,
                                     pval = TRUE,
                                     conf.int = TRUE,
                                     surv.median.line = "hv", # Specify median survival
                                     break.time.by = 1,
                                     facet.by = "sex",
                                     ggtheme = theme_bw(), # Change ggplot2 theme
                                     palette = "aaas",
                                     legend = "bottom",
                                     xlab = "Time (years)",
                                     ylab = "Death probability",
                                     panel.labs = list(sex_recoded=c("Male", "Female")),
                                     legend.labs = c("A", "B")
) 

plot_test

是时候发牢骚了。您可以使用
purr
完成所需操作。您可以阅读有关制作ggplot2
purrr
的内容以及更多示例

首先,我们需要使用
tidyr::gather
将数据转换为长格式。除了变量1、2、3、4之外,我们将保持数据帧中的所有内容不变。它们会融化

library(tidyr)
library(dplyr)
library(purrr)

data %>% 
  gather(num, variable, -sample_id,  -sex,
         -visit_number, -age_at_enrollment,
         -follow_up, -status) %>% 
  mutate(num2 = num) %>% # We'll need this column later for the titles
  as_tibble() -> long_data


# A tibble: 2,028 x 8
   sample_id   sex    visit_number age_at_enrollment follow_up status num       variable
   <fct>       <fct>  <fct>                    <dbl>     <dbl> <fct>  <chr>        <int>
 1 sample_0001 Female 1                         56.7     0     1      variable1        0
 2 sample_0001 Female 2                         57.7     0.920 1      variable1        0
 3 sample_0001 Female 3                         58.6     1.90  1      variable1        0
 4 sample_0001 Female 4                         59.7     2.97  2      variable1        0
 5 sample_0001 Female 5                         60.7     4.01  1      variable1        0
 6 sample_0001 Female 6                         61.7     4.99  1      variable1        0
 7 sample_0002 Female 1                         55.9     0     1      variable1        1
 8 sample_0002 Female 2                         56.9     1.04  1      variable1        1
 9 sample_0002 Female 3                         58.0     2.15  1      variable1        1
10 sample_0002 Female 4                         59.0     3.08  1      variable1        1
# ... with 2,018 more rows
现在,您可以通过键入以下命令返回绘图:

plots$plots[[1]]
plots$plots[[2]]
plots$plots[[3]] 
plots$plots[[4]] # plotted below

并使用
map2()


更新

不幸的是,我不知道如何更改图例标签。我能建议的唯一解决方案如下。请记住,
plots$plots[…]
是一个
ggplot
对象,因此您可以在之后更改所有内容。例如,要更改图例标签,我只需要添加
scale\u fill\u discrete
scale\u color\u discrete
。标题、实验、主题等也可以这样做

library(ggsci) # to add aaas color palette

plots$plots[[3]] +
  labs(title = "Variable 3",
       subtitle = "You just have to be the best") +
  ggsci::scale_color_aaas(guide = F) +
  ggsci::scale_fill_aaas(label = LETTERS[1:3])

问题可能与之前的帖子有关:。但是,即使我使用survminer::surv_fit()fx而不是survival::survfit()fx来生成拟合模型,问题仍然存在。亲爱的@atsyplenkov,这可能是另一个问题,但是您如何修改代码,使其显示每个图的标题(“variable1”、“variable2”…)考虑到有两种类型的绘图:包含两个类别(“A”、“B”)的绘图和包含三个类别(“A”、“B”、“C”)的绘图,您将如何修改图例标签?《传奇》是原帖的一部分。我现在觉得很难受。Thanks@Yatrosin,很遗憾,我不知道如何更改图例标签。这是非常棘手的,因为标签数量正在变化。要添加标题,只需在
ggsurvplot
a
ggtitle
函数后添加,该函数具有
+
功能,因为它是一个普通的
ggplot2
。我还创建了一个新列
num2
等于
num
。请参见答案。我以前尝试过使用.y$num作为标题,但无论是对于.y$num还是.y$num2,我总是会遇到以下错误:
警告消息:1:未知或未初始化列:“num”。2:未知或未初始化的列:“num”。3:未知或未初始化的列:“num”。4:未知或未初始化的列:“num”。
是否创建了
.y$num2
?尝试重新启动R并再次运行我的答案中的代码。亲爱的@Yatrosin,在我看来,最简单的方法是将
0,1,2
转换为
(“A”、“B”、“C”)
。手动添加到
绘图$plots[…]
的步骤。例如,对于变量3,它将类似于:
绘图$plots[[3]]+ggsci::scale\u color\u aaas(guide=F)+ggsci::scale\u fill\u aaas(label=LETTERS[1:3])
。我正在使用一个用于调色板的
ggsci
软件包,别忘了安装它。
library(tidyr)
library(dplyr)
library(purrr)

data %>% 
  gather(num, variable, -sample_id,  -sex,
         -visit_number, -age_at_enrollment,
         -follow_up, -status) %>% 
  mutate(num2 = num) %>% # We'll need this column later for the titles
  as_tibble() -> long_data


# A tibble: 2,028 x 8
   sample_id   sex    visit_number age_at_enrollment follow_up status num       variable
   <fct>       <fct>  <fct>                    <dbl>     <dbl> <fct>  <chr>        <int>
 1 sample_0001 Female 1                         56.7     0     1      variable1        0
 2 sample_0001 Female 2                         57.7     0.920 1      variable1        0
 3 sample_0001 Female 3                         58.6     1.90  1      variable1        0
 4 sample_0001 Female 4                         59.7     2.97  2      variable1        0
 5 sample_0001 Female 5                         60.7     4.01  1      variable1        0
 6 sample_0001 Female 6                         61.7     4.99  1      variable1        0
 7 sample_0002 Female 1                         55.9     0     1      variable1        1
 8 sample_0002 Female 2                         56.9     1.04  1      variable1        1
 9 sample_0002 Female 3                         58.0     2.15  1      variable1        1
10 sample_0002 Female 4                         59.0     3.08  1      variable1        1
# ... with 2,018 more rows
long_data %>% 
  group_by(num) %>% 
  nest() %>% 
  mutate(
    # Run survfit() for every variable
    fit_f = map(data, ~survfit(Surv(follow_up, as.numeric(status)) ~ (sex + variable), data = .)),
    # Create survplot for every variable and survfit
    plots = map2(fit_f, data, ~ggsurvplot(.x,
                                          as.data.frame(.y), # Important! convert from tibble to data.frame 
                                          pval = TRUE,
                                          conf.int = TRUE,
                                          facet.by = "sex",
                                          surv.median.line = "hv", 
                                          break.time.by = 1,
                                          ggtheme = theme_bw(),
                                          palette = "aaas",
                                          xlab = "Time (years)",
                                          ylab = "Death probability") +
                   ggtitle(paste0("This is plot of ", .y$num2)) + # Add a title
                   theme(legend.position = "bottom"))) -> plots
plots$plots[[1]]
plots$plots[[2]]
plots$plots[[3]] 
plots$plots[[4]] # plotted below
map2(paste0(unique(long_data$num), ".pdf"), plots$plots, ggsave)
library(ggsci) # to add aaas color palette

plots$plots[[3]] +
  labs(title = "Variable 3",
       subtitle = "You just have to be the best") +
  ggsci::scale_color_aaas(guide = F) +
  ggsci::scale_fill_aaas(label = LETTERS[1:3])