###### Load data and packages
Tabletext <- read.csv("~/Desktop/Test/Tabletext.csv", stringsAsFactors = F, na.strings = c("N/A", "", "NA")) # Import clean version of tabletext
Table <- read.csv("~/Desktop/Test/Test_Tabletext.csv", stringsAsFactors = F, na.strings = c("N/A", "", "NA")) # Import old tabletext for forestplot design
pacman::p_load(pacman, party, rio, tidyverse)
library(scales)
structure(list(X = c("Variable", NA, "Continuous", "Cell A",
" ", " ", " ", NA, "Cell B", NA, NA, NA, NA, "Cell C", NA, NA,
NA, NA, "Cell D", NA, NA, NA, NA), X.1 = c("Type", NA, NA, "Type1",
"Type2", "Type3", "Type4", "Unknown", "Type1", "Type2", "Type3",
"Type4", "Unknown", "Type1", "Type2", "Type3", "Type4", "Unknown",
"Type1", "Type2", "Type3", "Type4", "Unknown"), X.2 = c("OR (95%CI)",
NA, NA, "2.60e+230 (0.00e+00 -Inf)", "NA ", "1.23e+04 (0.00e+00 -1.75e+11)",
"3.59e+01 (3.10e-01 -4.13e+03)", "1.21e+01 (4.00e-02 -3.83e+03)",
"6.39e+12 (4.03e-18 -1.75e+43)", "NA ", "2.75e+00 (0.00e+00 -2.09e+03)",
"1.08e+01 (1.00e-01 -1.19e+03)", "5.06e+04 (1.00e-02 -2.54e+11)",
"Inf (0.00e+00 -Inf)", "NA ", "5.00e+02 (0.00e+00 -5.40e+09)",
"1.31e+01 (1.80e-01 -9.74e+02)", "4.90e-01 (0.00e+00 -4.62e+02)",
"3.44e+01 (6.00e-02 -1.82e+04)", "NA ", "2.74e+01 (0.00e+00 -1.90e+05)",
"1.23e+01 (1.40e-01 -1.04e+03)", "3.83e+09 (3.51e-05 -2.96e+23)"
), X.3 = c("p-value", NA, NA, "1", " NA", "0.23", "0.14", "0.4",
"0.4", " NA", "0.88", "0.32", "0.17", "1", " NA", "0.39", "0.24",
"0.84", "0.27", " NA", "0.5", "0.27", "0.18"), X.4 = c("FDR",
NA, NA, "1", " NA", "0.67", "0.32", "0.53", "0.81", " NA",
"0.88", "0.32", "0.36", "1", " NA", "0.67", "0.32", "0.84",
"0.81", " NA", "0.67", "0.32", "0.36")), row.names = c(NA, -23L
), class = "data.frame")
structure(list(Variable = c(NA, "Continuous", "Cell A", " ",
" ", " ", NA, "Cell B", NA, NA, NA, NA, "Cell C", NA, NA, NA,
NA, "Cell D", NA, NA, NA, NA), Type = c(NA, NA, "Type1", "Type2",
"Type3", "Type4", "Unknown", "Type1", "Type2", "Type3", "Type4",
"Unknown", "Type1", "Type2", "Type3", "Type4", "Unknown", "Type1",
"Type2", "Type3", "Type4", "Unknown"), OR = c(NA, NA, 2.6e+230,
NA, 12300, 35.9, 12.1, 6.39e+12, NA, 2.75, 10.8, 50600, Inf,
NA, 500, 13.1, 0.494, 34.4, NA, 27.4, 12.3, 3.83e+08), Lower.95.CI = c(NA,
NA, 0, NA, 0.00178, 0.311, 0.0385, 4.03e-18, NA, 0.00146, 0.0975,
0.0101, 0, NA, 0.00015, 0.177, 0.000529, 0.0647, NA, 0.00265,
0.145, 3.51e-05), Upper.95.CI = c(NA, NA, Inf, NA, 1.75e+11,
4130, 3830, 1.75e+43, NA, 2090, 1190, 2.54e+11, Inf, NA, 5.4e+09,
974, 462, 18200, NA, 190000, 1040, 2.96e+23), p.value = c(NA,
NA, 0.998861302, NA, 0.234223384, 0.139379426, 0.395286095, 0.403434934,
NA, 0.87711484, 0.321803077, 0.168744949, 0.998407711, NA, 0.392944826,
0.241531723, 0.839916941, 0.26922861, NA, 0.500190621, 0.268220999,
0.181966888), FDR = c(NA, NA, 0.998861302, NA, 0.666920828, 0.321803077,
0.527048126, 0.806869868, NA, 0.87711484, 0.321803077, 0.363933776,
0.998861302, NA, 0.666920828, 0.321803077, 0.839916941, 0.806869868,
NA, 0.666920828, 0.321803077, 0.363933776)), row.names = c(NA,
-22L), class = "data.frame")
>
# Formatting numbers to 2 decimal places:
Table$OR <- as.numeric(scientific(Table$OR, digits = 3))
Table$Lower.95.CI <- as.numeric(scientific(Table$Lower.95.CI, digits = 3))
Table$Upper.95.CI <- as.numeric(scientific(Table$Upper.95.CI, digits = 3))
Forest_lines <- Table[,c(3:5)] # Separating out OR, 95% Upper and lower interval columns.
# Building this data frame row by row to for the aforementioned 3 variables in such a way to match row items in 'Tabletext'. This will allow for design of forest lines in the forest plot:
Forest_lines <- rbind(c(NA,NA,NA), c(NA,NA,NA), Forest_lines[c(3:23),])
fn <- local({
i = 0
no_lines <- sum(!is.na(Forest_lines$OR))
b_clrs = colorRampPalette(colors=c("orange", "purple", "green", "grey"))(no_lines)
function(..., clr.marker){
i <<- i + 1
fpDrawNormalCI(..., clr.marker = b_clrs[i])
}
})
###Graphing the forest plot:
library(forestplot)
png("~/Desktop/Test.png", width = 1200, height = 800)
forestplot(Tabletext,
Forest_lines,
fn.ci_norm = fn,
new_page = T,
is.summary=c(T, F, T, rep(FALSE, 23)),
clip=c(0.01,50),
xlog=T,
graph.pos = 3,
boxsize = 0.3,
xticks = c(0.01, 0.1, 0.5, 1, 2, 10, 90),
xlab = "
Decreased value Increased value",
txt_gp = fpTxtGp(label = gpar(fontfamily = "", cex=.95), ticks = gpar(cex=1.1), xlab = gpar(cex = 1.1)),
title = "Test")
dev.off()
However, I think there is an issue with the colour function in my code. Essentially, I would like all Type 1 boxes to appear as orange, all Type 2 boxes to appear as purple and so on. Any suggestions on how I can modify my code in order to assign a single colour to each box corresponding to to individual "Types"?