繁体   English   中英

适配森林 Plot R 脚本

[英]Adapting Forest Plot R Script

我是一名新手编码员,一直试图理解此处发布的代码: Forest plot with table ggplot coding

我希望使用该脚本来显示我自己的项目单变量分析结果。 我希望脚本从 csv 文件中读取数据,其中包含以下列:“Predictor”、“N”、“rr”、“rrlow”、“rrhigh”和“arr”。 我需要显示总共 19 个变量(“预测变量”)。 我已更改脚本以将值读入单个 dataframe (而不是像链接线程中那样具有单独的 forestdf 和 fplottable )。 但是,我得到多个“替换有 x 行,数据有 y”。

这是有问题的代码:

###dataframe
library(ggplot2)
library(tidyr)
library(grid)
library(gridExtra)
library(forcats)

forestdf<- read.csv("UnivariateAnalysis2.csv",header=T)
forestdf$Predictor <- factor(forestdf$Predictor,levels = forestdf$Predictor)
levels(forestdf$Predictor)  
forestdf$colour <- rep(c("white", "gray95"), length.out = 19)
p <- ggplot(forestdf, aes(x = rr, y = Predictor, xmin = rrlow, xmax = rrhigh)) +
  geom_hline(aes(yintercept = 1, colour = colour), size = 7) + 
  geom_pointrange(shape = 22, fill = "black") +
  geom_vline(xintercept = 1, linetype = 3) +
  xlab("Variable") +
  ylab("Hazard Ratio with 95% Confidence Interval") +
  theme_classic() +
  scale_colour_identity() +
  scale_y_discrete(limits = rev(forestdf$Predictor)) +
  scale_x_log10(limits = c(0.25, 4), 
                breaks = c(0.25, 0.5, 1, 2, 4), 
                labels = c("0.25", "0.5", "1", "2", "4"), expand = c(0,0)) +
  theme(axis.text.y = element_blank(), axis.title.y = element_blank())

forestdf$Predictor <- factor(forestdf$Predictor, rev(levels(forestdf$Predictor)))
forestdf$colour <- rep(c("white", "gray95"), length.out=19)

data_table <- ggplot(data = forestdf, aes(y = Predictor)) +
  geom_hline(aes(yintercept = 1, colour = colour), size = 7) +
  geom_text(aes(x = 0, label = Predictor), hjust = 0) +
  geom_text(aes(x = 5, label = N)) +
  geom_text(aes(x = 7, label = arr), hjust = 1) +
  scale_colour_identity() +
  theme_void() + 
  theme(plot.margin = margin(5, 0, 35, 0))

grid.arrange(data_table,p, ncol = 2)

我收到的错误:

> ###dataframe
> library(ggplot2)
> library(tidyr)
> library(grid)
> library(gridExtra)
> library(forcats)
> 
> forestdf<- read.csv("UnivariateAnalysis2.csv",header=T)
> forestdf$Predictor <- factor(forestdf$Predictor,levels = forestdf$Predictor)
Error in `$<-.data.frame`(`*tmp*`, Predictor, value = integer(0)) : 
  replacement has 0 rows, data has 19
> levels(forestdf$Predictor)  
NULL
> forestdf$colour <- rep(c("white", "gray95"), length.out = 19)
> p <- ggplot(forestdf, aes(x = rr, y = Predictor, xmin = rrlow, xmax = rrhigh)) +
+   geom_hline(aes(yintercept = 1, colour = colour), size = 7) + 
+   geom_pointrange(shape = 22, fill = "black") +
+   geom_vline(xintercept = 1, linetype = 3) +
+   xlab("Variable") +
+   ylab("Hazard Ratio with 95% Confidence Interval") +
+   theme_classic() +
+   scale_colour_identity() +
+   scale_y_discrete(limits = rev(forestdf$Predictor)) +
+   scale_x_log10(limits = c(0.25, 4), 
+                 breaks = c(0.25, 0.5, 1, 2, 4), 
+                 labels = c("0.25", "0.5", "1", "2", "4"), expand = c(0,0)) +
+   theme(axis.text.y = element_blank(), axis.title.y = element_blank())
> 
> forestdf$Predictor <- factor(forestdf$Predictor, rev(levels(forestdf$Predictor)))
Error in `$<-.data.frame`(`*tmp*`, Predictor, value = integer(0)) : 
  replacement has 0 rows, data has 19
> forestdf$colour <- rep(c("white", "gray95"), length.out=19)
> 
> data_table <- ggplot(data = forestdf, aes(y = Predictor)) +
+   geom_hline(aes(yintercept = 1, colour = colour), size = 7) +
+   geom_text(aes(x = 0, label = Predictor), hjust = 0) +
+   geom_text(aes(x = 5, label = N)) +
+   geom_text(aes(x = 7, label = arr), hjust = 1) +
+   scale_colour_identity() +
+   theme_void() + 
+   theme(plot.margin = margin(5, 0, 35, 0))
> 
> grid.arrange(data_table,p, ncol = 2)
Error in FUN(X[[i]], ...) : object 'Predictor' not found

我非常感谢您提供的任何帮助或建议。

谢谢!

编辑:

###dataframe
library(ggplot2)
library(tidyr)
library(grid)
library(gridExtra)
library(forcats)

forestdf<- read.csv("UnivariateAnalysis2.csv",header=TRUE)
names(forestdf)[1]<-"Predictor"
forestdf$Predictor <- factor(forestdf$Predictor)
forestdf$colour <- rep(c("white", "gray95"), length.out = length(unique(unlist(forestdf[c("Predictor")]))))
p <- ggplot(forestdf, aes(x = rr, y = Predictor, xmin = rrlow, xmax = rrhigh)) +
  geom_hline(aes(yintercept = Predictor, colour = colour), size = 7) + 
  geom_pointrange(shape = 22, fill = "black") +
  geom_vline(xintercept = 1, linetype = 3, colour = "red") +
  xlab("Hazard Ratio") +
  ylab("Hazard Ratio with 95% Confidence Interval") +
  theme_classic() +
  scale_colour_identity() +
  scale_y_discrete(limits = rev(forestdf$Predictor)) +
  scale_x_log10(limits = c(0.25, 4), 
                breaks = c(0.25, 0.5, 1, 2, 4), 
                labels = c("0.25", "0.5", "1", "2", "4"), expand = c(0,0)) +
  theme(axis.text.y = element_blank(), axis.title.y = element_blank())

forestdf$Predictor <- factor(forestdf$Predictor, rev(levels(forestdf$Predictor)))

data_table <- ggplot(data = forestdf, aes(y = Predictor)) +
  geom_hline(aes(yintercept = Predictor, colour = colour), size = 7) +
  geom_text(aes(x = 0, label = Predictor), hjust = 0) +
  geom_text(aes(x = 3, label = N)) +
  geom_text(aes(x = 7, label = arr), hjust = 1) +
  scale_colour_identity() +
  theme_void() + 
  theme(plot.margin = margin(5, 0, 35, 0))

grid.arrange(data_table,p, ncol = 2)

我根据 IRTFM 做了一些更改(谢谢。)它现在生成了 plot 和表格。 我不知道为什么,但它没有正确读取 csv:我现在的主要问题如下:

  1. 交替的灰色和白色条在桌子一侧没有正确交替
  2. 列的 header 未显示在表上
  3. 该表未与森林图对齐(即顶行的森林 plot 不是白蛋白的正确森林 plot)示例 Plot

编辑2:

我能够用森林图修复交替颜色和 alignment。 我现在的问题是我制作的专栏标题现在被切断了: New Plot 另外,我将如何 go 只用星号加粗值?

###dataframe
library(ggplot2)
library(tidyr)
library(grid)
library(gridExtra)
library(forcats)

forestdf<- read.csv("UnivariateAnalysis2.csv",header=TRUE)
names(forestdf)[1]<-"Predictor"
forestdf$Predictor <- rev(factor(forestdf$Predictor))
forestdf$colour <- rep(c("white", "gray95"), length.out = length(unique(unlist(forestdf[c("Predictor")]))))
p <- ggplot(forestdf, aes(x = rr, y = Predictor, xmin = rrlow, xmax = rrhigh)) +
  geom_hline(aes(yintercept = Predictor, colour = colour), size = 7) + 
  geom_pointrange(shape = 22, fill = "black") +
  geom_vline(xintercept = 1, linetype = 3, colour = "red") +
  xlab("Hazard Ratio") +
  ylab("Hazard Ratio with 95% Confidence Interval") +
  theme_classic() +
  scale_colour_identity() +
  scale_y_discrete(limits = forestdf$Predictor) +
  scale_x_log10(limits = c(0.25, 4), 
                breaks = c(0.25, 0.5, 1, 2, 4), 
                labels = c("0.25", "0.5", "1", "2", "4"), expand = c(0,0)) +
  theme(axis.text.y = element_blank(), axis.title.y = element_blank())

#forestdf$Predictor <- factor(forestdf$Predictor, rev(levels(forestdf$Predictor)))

data_table <- ggplot(data = forestdf, aes(y = rev(factor(Predictor)))) +
  geom_hline(aes(yintercept = Predictor, colour = colour), size = 7) +
  geom_text(aes(x = 0, label = Predictor), show.legend=TRUE, hjust = 0) +
  geom_text(aes(x = 3, label = N)) +
  geom_text(aes(x = 5.5, label = arr), hjust = 1) +
  geom_text(aes(x = 7, label = PVALUE), hjust = 1) +
  geom_text(aes(x = 0, y = 20, label = "Predictor"), hjust = 0) +
  geom_text(aes(x = 3, y= 20, label = "N")) +
  geom_text(aes(x = 5, y= 20, label = "95% CI"), hjust = 1) +
  geom_text(aes(x = 7, y= 20, label = "P Value"), hjust = 1) +
  scale_colour_identity() +
  theme_void() + 
  theme(plot.margin = margin(5, 0, 35, 0))
  
grid.arrange(data_table,p, ncol = 2)

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM