在R中计算和可视化“转移时间”

xam8gpfp  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(104)

我对R很陌生,我发现自己正在努力解决以下问题。
我想创建一个条形图,其中包含2个条形,用于显示100名患者中40名患者发生转移的时间。一个条形代表接受免疫治疗的患者,另一个条形代表未接受免疫治疗的患者。

  • 100名患者
  • 40例随时间推移发生转移
  • 30例接受免疫治疗

我创建了一个excel_file,其中包含以下列:
1.转移(是= 1,否= 0)
1.时间(如果发现转移(因此之前的列中为1),任何数字(1,2,3,…)
1.免疫治疗(是= 1,否= 0)
基本上我必须
1.仅过滤发生转移的患者(1)

  • 创建条形图:
  • y轴:TTM(转移时间)
  • x轴:免疫疗法(1巴-是,1巴-否)

我很感激任何帮助,
亲切的问候

ggplot(data = M1, aes (x = Immunotherapy, y = Time))+ geom_col()
efzxgjgh

efzxgjgh1#

**更新2:**参见OP问题2评论:

我们可以使用facet_wrap。我在M1中添加了一列Cancer_Type:新数据:

M1 <- structure(list(Metastasis = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1), time = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 3, 5, 1, 3, 5, 8, 6, 7, 10, 7, 9, 9, 9, 3, 
2, 5, 9, 4, 9, 6, 8, 1, 6, 9, 6, 10, 6, 6, 4, 7, 6, 10, 5, 5, 
2, 9, 6, 1, 1, 2), Immunotherapy = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1), Cancer_Type = c("Colon cancer", "Lung cancer", 
"Colon cancer", "Lung cancer", "Colon cancer", "Colon cancer", 
"Colon cancer", "Lung cancer", "Colon cancer", "Colon cancer", 
"Colon cancer", "Lung cancer", "Colon cancer", "Lung cancer", 
"Lung cancer", "Colon cancer", "Lung cancer", "Colon cancer", 
"Colon cancer", "Lung cancer", "Colon cancer", "Lung cancer", 
"Lung cancer", "Colon cancer", "Colon cancer", "Lung cancer", 
"Lung cancer", "Colon cancer", "Lung cancer", "Lung cancer", 
"Lung cancer", "Lung cancer", "Lung cancer", "Colon cancer", 
"Colon cancer", "Colon cancer", "Colon cancer", "Lung cancer", 
"Colon cancer", "Lung cancer", "Lung cancer", "Colon cancer", 
"Colon cancer", "Colon cancer", "Lung cancer", "Lung cancer", 
"Colon cancer", "Colon cancer", "Colon cancer", "Lung cancer", 
"Lung cancer", "Colon cancer", "Lung cancer", "Colon cancer", 
"Colon cancer", "Lung cancer", "Lung cancer", "Lung cancer", 
"Colon cancer", "Lung cancer", "Colon cancer", "Lung cancer", 
"Lung cancer", "Lung cancer", "Colon cancer", "Colon cancer", 
"Lung cancer", "Lung cancer", "Colon cancer", "Lung cancer", 
"Colon cancer", "Lung cancer", "Colon cancer", "Colon cancer", 
"Colon cancer", "Colon cancer", "Lung cancer", "Colon cancer", 
"Lung cancer", "Colon cancer", "Colon cancer", "Colon cancer", 
"Lung cancer", "Colon cancer", "Colon cancer", "Colon cancer", 
"Colon cancer", "Lung cancer", "Lung cancer", "Lung cancer", 
"Colon cancer", "Colon cancer", "Colon cancer", "Lung cancer", 
"Lung cancer", "Lung cancer", "Lung cancer", "Colon cancer", 
"Colon cancer", "Lung cancer")), row.names = c(NA, -100L), class = "data.frame")

代码:facet_wrap()

M1 %>% 
  mutate(Immunotherapy = factor(as.character(Immunotherapy), labels = c("no", "yes"))) %>% 
  filter(Metastasis >=1) %>% 
  ggplot(aes(x = Immunotherapy, y = time, fill = Immunotherapy)) +
  geom_boxplot(outlier.shape = NA, alpha = 0.8, color = "black") +
  geom_jitter(width = 0.2, alpha = 0.5, size = 3) +
  scale_fill_manual(values = c("#E69F00", "#56B4E9")) +
  labs(x = "Immunotherapy", y = "Time to metastasis (months)") +
  facet_wrap(~Cancer_Type)+
  theme_classic()+
  scale_y_continuous(breaks = seq(0, ceiling(max(M1$time)), by = 2))


**更新1:**OP问题见注解:

问题1:

  • 框的上方和下方的线是所谓的盒须图
  • 它们表示框外的数据范围。
  • 晶须的长度通常是IQR的1.5倍。
  • 须线以外的任何数据点均视为离群值,并绘制为单个点。
    **问题2:**我想你指的是免疫疗法而不是转移:

使用这一行:mutate(Immunotherapy = factor(as.character(Immunotherapy), labels = c("no", "yes")))
我们将免疫疗法(0,1)从数字转换为因子。因子变量具有水平和标签。因此,我们可以像上一行中所做的那样为因子分配免疫疗法标签,例如“no”和“yes”。现在是关键时刻,它是自然数的顺序。因此0在1之前,因此“no”(在“yes”之前的上一行中)和“yes”是1。

第一个答案我们可以这样做:

library(dplyr)
library(ggplot2)

# create fake data
M1 <- data.frame(
  Metastasis = c(rep(0, 60), rep(1, 40)),
  time = c(rep(0, 60), sample(1:10, 40, replace = TRUE)),
  Immunotherapy = c(rep(0, 70), rep(1, 30))
)


M1 %>% 
  mutate(Immunotherapy = factor(as.character(Immunotherapy), labels = c("no", "yes"))) %>% 
  filter(Metastasis >=1) %>% 
  ggplot(aes(x = Immunotherapy, y = time, fill = Immunotherapy)) +
  geom_boxplot(outlier.shape = NA, alpha = 0.8, color = "black") +
  geom_jitter(width = 0.2, alpha = 0.5, size = 3) +
  scale_fill_manual(values = c("#E69F00", "#56B4E9")) +
  labs(x = "Immunotherapy", y = "Time to metastasis (months)") +
  theme_classic()+
  scale_y_continuous(breaks = seq(0, ceiling(max(M1$time)), by = 2))

相关问题