Select columns from multiple rda files R and save the selected output - r

I would like to select (by names or position) specified columns in multiple .rda files stored in a directory and then save them only with the selected columns (preferably using purrr and dplyr).
The .rda files have all the same structure: 38 columns, first 28 columns have the same names, the last 10 columns have different names with the same prefix ("SC*").
This is what I have tried so far:
library(tidyverse)
file_names <- as.list(dir(path=".", pattern="ASJC*"))
I load the files selected
files <- map(file_names,load,environment())
Then I try to select specified columns by names:
files_selected <- map(files,dplyr::select(SOURCERECORD_ID,starts_with("SC*")))
and I got the following error message:
Error in dplyr::select(SOURCERECORD_ID, starts_with("SC*")) :
object 'SOURCERECORD_ID' not found
So i tried the following code:
files_selected <- map(files,dplyr::select(1,29:38)
with another error message:
Error in UseMethod("select_") :
no applicable method for 'select_' applied to an object of class "c('double', 'numeric')"
This is the structure of .rda files:
df1 <- structure(list(SOURCERECORD_ID = c("18659", "13951", "5400152705",
"16500154707", "20300195074", "19472"), TITOLO_FONTE = c("ANAIS DA ACADEMIA BRASILEIRA DE CIENCIAS",
"ARABIAN JOURNAL FOR SCIENCE AND ENGINEERING", "ARCHIVES DES SCIENCES",
"ASIAN JOURNAL OF SCIENTIFIC RESEARCH", "ASM SCIENCE JOURNAL",
"BEIJING DAXUE XUEBAO (ZIRAN KEXUE BAN)/ACTA SCIENTIARUM NATURALIUM UNIVERSITATIS PEKINENSIS"
), ISSN_P = c("0001-3765", "1319-8025", "1661-464X", "1992-1454",
"1823-6782", "0479-8023"), ISSN_E = c("1678-2690", NA, NA, NA,
NA, NA), STATUS = c("Active", "Active", "Active", "Active", "Active",
"Active"), COPERTURA = c("1994-ongoing, 1970-1992, 1949", "2003-ongoing, 1981",
"2017-ongoing, 2004-2014", "2009-ongoing", "2011-ongoing", "2001-ongoing"
), LINGUA = c("ENG", "ENG", "ENG, FRE", "ENG", "ENG", "CHI"),
CS2014 = c(0.95, 1.19, 0.64, 0.55, 0.1, 0.24), CS2015 = c(0.89,
0.81, 0.57, 0.36, 0.06, 0.17), CS2016 = c(1.05, 1.02, NA,
0.64, 0.11, 0.35), SJR2014 = c(0.42, 0.332, 0.285, 0.394,
0.107, 0.13), SJR2015 = c(0.332, 0.335, 0.201, 0.163, 0.122,
0.123), SJR2016 = c(0.386, 0.29, 0.149, 0.195, 0.101, 0.157
), SNIP2014 = c(0.756, 1.149, 0.236, 1.021, 0.408, 0.338),
SNIP2015 = c(0.67, 0.51, 0.362, 0.472, 0.082, 0.164), SNIP2016 = c(0.713,
0.657, 0.275, 0.549, 0.595, 0.265), TIPO_FONTE = c("Journal",
"Journal", "Journal", "Journal", "Journal", "Journal"), STORIA_TITOLO = c(NA,
NA, "Formerly known as", NA, NA, NA), TITOLI_COLLEGATI = c(NA,
NA, "Archives des Sciences et Compte Rendu Seances de la Societe",
NA, NA, NA), EDITORE = c("Academia Brasileira de Ciencias",
"Springer Verlag", "Societe de physique et d'histoire naturelle",
"Asian Network for Scientific Information", "Akademi Sains Malaysia",
"Beijing University Press"), PAESE_EDITORE = c("Brazil",
"Germany", "Switzerland", "Pakistan", "Malaysia", "China"
), ASJC = c("1000;", "1000;", "1000;", "1000;", "1000;",
"1000;"), AVG_CS = c(0.963, 1.007, 0.605, 0.517, 0.09, 0.253
), AVG_SJR = c(0.379, 0.319, 0.212, 0.251, 0.11, 0.137),
AVG_SNIP = c(0.713, 0.772, 0.291, 0.681, 0.362, 0.256), ELEGGIBILE = c("Eleggibile",
"Eleggibile", "Eleggibile", "Eleggibile", "Eleggibile", "Eleggibile"
), Percentili_SJR = c(15L, 21L, 47L, 35L, 89L, 77L), Percentili_SNIP = c(25L,
19L, 74L, 28L, 61L, 78L), SC13A_1000_SJR = c("Bottom", "Bottom",
"Bottom", "Bottom", "Bottom", "Bottom"), SC13B_1000_SJR = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13C_1000_SJR = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13D_1000_SJR = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13D4_1000_SJR = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13A_1000_SNIP = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13B_1000_SNIP = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13C_1000_SNIP = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13D_1000_SNIP = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom"), SC13D4_1000_SNIP = c("Bottom",
"Bottom", "Bottom", "Bottom", "Bottom", "Bottom")), .Names = c("SOURCERECORD_ID",
"TITOLO_FONTE", "ISSN_P", "ISSN_E", "STATUS", "COPERTURA", "LINGUA",
"CS2014", "CS2015", "CS2016", "SJR2014", "SJR2015", "SJR2016",
"SNIP2014", "SNIP2015", "SNIP2016", "TIPO_FONTE", "STORIA_TITOLO",
"TITOLI_COLLEGATI", "EDITORE", "PAESE_EDITORE", "ASJC", "AVG_CS",
"AVG_SJR", "AVG_SNIP", "ELEGGIBILE", "Percentili_SJR", "Percentili_SNIP",
"SC13A_1000_SJR", "SC13B_1000_SJR", "SC13C_1000_SJR", "SC13D_1000_SJR",
"SC13D4_1000_SJR", "SC13A_1000_SNIP", "SC13B_1000_SNIP", "SC13C_1000_SNIP",
"SC13D_1000_SNIP", "SC13D4_1000_SNIP"), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))

Are you sure that files <- map(file_names,load,environment()) holds a list of datframes?
If yes, then try this
map(files,~ dplyr::select(.,SOURCERECORD_ID,starts_with("SC*")))
I think what's missing is the ~ before the select function, and the . in the select function since select is select(.data, ...). By.` you imply here is where the each dataset goes

Related

Pie Chart using R

I want to create a piechart (showing Forest.Area.ha. by GaPa_NaPa) based on following attribute table as below:-
The dataframe for the data is as shown:-
structure(list(GaPa_NaPa = c("Gaidahawa", "Kanchan", "Kotahimai",
"Marchawari", "Mayadevi", "Omsatiya", "Rohini", "Sammarimai",
"Siyari", "Sudhdhodhan", "Devdaha", "Lumbini Sanskritik", "Sainamaina",
"Siddharthanagar", "Tillotama", "Butwal"), Total.Area..ha. = c(9657L,
5835L, 5812L, 4844L, 7228L, 4844L, 6449L, 5066L, 6620L, 5743L,
13667L, 11194L, 16082L, 3595L, 12592L, 10139L), Forest.Area.ha. = c(114.91,
178.19, 31.37, 43.43, 152.87, 29.12, 63.16, 59.81, 36.4, 16.42,
113.13, 422.87, 186.13, 167.2, 60.27, 45.3), Forest.Percent = c(6.67,
10.35, 1.83, 2.52, 8.88, 1.69, 3.67, 3.47, 2.11, 0.95, 6.57,
24.57, 10.81, 9.71, 3.5, 2.63), Forest.Area..Fraction. = c(0.07,
0.1, 0.02, 0.03, 0.09, 0.02, 0.04, 0.03, 0.02, 0.01, 0.07, 0.25,
0.11, 0.1, 0.04, 0.03), Household.No = c(8612L, 9828L, 5939L,
5305L, 8003L, 6683L, 6349L, 5164L, 7889L, 7619L, 15624L, 10736L,
17572L, 12329L, 30452L, 36989L), Family.Size = c(10020L, 10483L,
7921L, 6972L, 10040L, 8218L, 8096L, 7303L, 9060L, 8717L, 17582L,
13854L, 19657L, 16011L, 36399L, 51099L), Total = c(56529L, 42528L,
46417L, 41058L, 57341L, 41080L, 43277L, 43300L, 45274L, 41472L,
71806L, 88090L, 78477L, 76307L, 149657L, 195054L)), row.names = c(NA,
16L), class = "data.frame")
The code I used is:-
setwd("C:/Users/lenovo/Desktop/AllAboutR/AssignmentDocs")
ForestArea2010<-read.csv("Forest2010.csv")
View(ForestArea2010)
pie(RupandehiLULC19$GaPa_NaPa, main="Piechart of Forest Area", las=3, col=hsv(12))
But I couldn't work further on how to show piechart showing Forest.Area.ha. by GaPa_NaPa working on code to plot piechart. Please help on it. How the code must be written?
Here is another option with ggplot2. If you don't want the labels then the geom_text_repel line can be removed.
library(ggplot2)
library(ggrepel)
ggplot(RupandehiLULC19, aes(x = "", y = `Forest.Area.ha.`, fill = GaPa_NaPa)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0) +
xlab("") +
ylab("Piechart of Forest Area") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
geom_text_repel(aes(label = `Forest.Area.ha.`),
position = position_stack(vjust = 0.5))
Output
Or another option using legend:
pie(RupandehiLULC19$Forest.Area.ha., labels = "", main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")
Or with the values:
pie(RupandehiLULC19$Forest.Area.ha., labels = RupandehiLULC19$Forest.Area.ha., main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")

Change font of specific rows to bold in forestplot

I wrote a script using the "forestplot" package. I want to group the variables in certain categories, which I would like to show in bold, in order to accentuate those categories. How can i adjust my script, so that only certain rows, i.e Risk factor OR (95% CI), patient characteristics, medication history, comorbidities, surgical history and other are shown in bold? I have two colums and 18 rows. Can someone help me? I would be much grateful!!
My script is as below:
tabletext <- cbind(
c("Risk factor" ,"Patient characteristics","Sex, male*", "Bmi (5 points)",
"Alcohol (5 units)", "Smoking*","Medication history",
"Steroid use", "Anticoagulant use*","Comorbidities",
"COPD GOLD 1/2", "COPD GOLD 3/4", "Other pulmonary disease",
"Surgical history",
"Previous colorectal surgery*",
"Previous abdominal surgery (other)","Other", "HIPEC*"),
c("OR (95% CI)",NA, "1.78 (1.20-2.68)", "1.15 (0.95-1.38)", "1.04 (0.94-1.14)",
"1.78 (1.11-2.80)", NA," 1.40 (0.68-2.67)", "1.55 (1.02-2.32)",NA,
"1.40 (0.70-2.61)", "1.56 (0.42-4.67)", "1.78 (0.63-4.28)",NA,
"1.61 (1.03-2.49)", "0.80 (0.47-1.32)",NA, "4.14 (2.14-7.73)"))
?fpTxtGp
require(forestplot)
forestplot(tabletext,
txt_gp = fpTxtGp(label = list(gpar(fontfamily = "Times",
fontface="bold"),
gpar(fontfamily = "",
col = "black"))),
df_c,new_page = TRUE,
boxsize = 0.2,
is.summary = c(rep(FALSE,32)),
clip = c(0,17),
xlab = 'Odds ratio with 95% confidence interval
* indicates significance',
xlog = FALSE,
zero = 1,
plotwidth=unit(12, "cm"),
colgap=unit(2, "mm"),
col = fpColors(box = "royalblue",
line = "darkblue",
summary = "royalblue"))
Its not clear what df_c is so I just created it based on your tabletext matrix:
df_c <- data.frame(mean = c(NA, NA, 1.78, 1.15, 1.04, 1.78, NA, 1.4, 1.55,
NA, 1.4, 1.56, 1.78, NA, 1.61, 0.8, NA, 4.14),
lower = c(NA, NA, 1.2, 0.95, 0.94, 1.11, NA, 0.68, 1.02, NA, 0.7,
0.42, 0.63, NA, 1.03, 0.47, NA, 2.14),
upper = c(NA, NA, 2.68, 1.38,1.14, 2.8, NA, 2.67,2.32, NA,
2.61, 4.67, 4.28, NA, 2.49, 1.32, NA, 7.73))
From there, its just a matter of adjusting the values passed to is.summary:
forestplot(tabletext,
txt_gp = fpTxtGp(label = list(gpar(fontfamily = "Times"),
gpar(fontfamily = "",
col = "black"))),
df_c,new_page = TRUE,
boxsize = 0.2,
is.summary = c(TRUE, TRUE, rep(FALSE, 4),
TRUE, FALSE, FALSE, TRUE,
rep(FALSE,3), TRUE, rep(FALSE,4)),
clip = c(0,17),
xlab = 'Odds ratio with 95% confidence interval
* indicates significance',
xlog = FALSE,
zero = 1,
plotwidth=unit(12, "cm"),
colgap=unit(2, "mm"),
col = fpColors(box = "royalblue",
line = "darkblue",
summary = "royalblue"))
Which generates the following figure:

R package "forestplot": Error message re: identifying the upper/lower boundaries from the input matrix

I'd be grateful for your help. I'm using the forestplot R package to create a forest plot. It was going well.
library(forestplot)
test_data <- data.frame(
coef = c(1.53, 1.56, 1.11, 1.35, 1.00, 0.94, 1.03),
low = c(1.43, 1.38, 1.08, 1.05, 0.91, 0.87, 0.95),
high = c(1.64, 1.76, 1.15, 1.75, 1.10, 1.01, 1.11),
boxsize = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
)
row_names <- cbind(
c("Variable", "BMI", "WHR", "Diabetes", "Insulin resistance", "LDL", "HDL", "ApoB"),
c("SNPs", "807", "368", "386", "53", "213", "510", "241"),
c("OR (95% CI)", "1·53 (1·43−1·64)", "1·56 (1·38−1·76)", "1·11 (1·08−1·15)", "1·35 (1·05−1·75)", "1·00 (0·91−1·10)", "0·94 (0·87−1·01)", "1·03 (0·95−1·11)"),
c("p-value", "6·72E−35", "2·90E−12", "6·61E−10", "0·02", "0·97", "0·083", "0·53")
)
test_data <- rbind(rep(NA, 3), test_data)
forestplot(
labeltext = row_names,
xlab = "Odds ratio",
test_data[, c("coef", "low", "high")],
is.summary = FALSE,
boxsize = test_data$boxsize,
zero = 1,
cex = 0.1,
align = TRUE,
xlog = FALSE,
graphwidth = unit(1.5, "snpc"),
col = fpColors(lines = "royalblue", box = "darkblue")
)
I've been adding one variable at a time and plotting my graph after the addition of each to make sure there were no errors introduced at each step. It was all going well until I added my eighth variable, "Triglycerides", and its associated numbers:
library(forestplot)
test_data <- data.frame(
coef = c(1.53, 1.56, 1.11, 1.35, 1.00, 0.94, 1.03, 1.15),
low = c(1.43, 1.38, 1.08, 1.05, 0.91, 0.87, 0.95, 1.07),
high = c(1.64, 1.76, 1.15, 1.75, 1.10, 1.01, 1.11, 1.11),
boxsize = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
)
row_names <- cbind(
c("Variable", "BMI", "WHR", "Diabetes", "Insulin resistance", "LDL", "HDL", "ApoB", "Triglycerides"),
c("SNPs", "807", "368", "386", "53", "213", "510", "241", "427"),
c("OR (95% CI)", "1·53 (1·43−1·64)", "1·56 (1·38−1·76)", "1·11 (1·08−1·15)", "1·35 (1·05−1·75)", "1·00 (0·91−1·10)", "0·94 (0·87−1·01)", "1·03 (0·95−1·11)", "1·15 (1·07−1·11)"),
c("p-value", "6·72E−35", "2·90E−12", "6·61E−10", "0·02", "0·97", "0·083", "0·53", "2·14E−4")
)
test_data <- rbind(rep(NA, 3), test_data)
forestplot(
labeltext = row_names,
xlab = "Odds ratio",
test_data[, c("coef", "low", "high")],
is.summary = FALSE,
boxsize = test_data$boxsize,
zero = 1,
cex = 0.1,
align = TRUE,
xlog = FALSE,
graphwidth = unit(1.5, "snpc"),
col = fpColors(lines = "royalblue", box = "darkblue")
)
On adding this eighth variable, I now receive the following error message:
Error in prFpConvertMultidimArray(mean) :
Sorry did not manage to correctly identify the upper/lower boundaries from the input matrix.
I can't spot a syntax error here, and I'm really at a loss as to why this has only just happened. Any advice would be greatly appreciated.
Thank you.

Forest plot with table ggplot coding

I am trying to get a table side by side with my forest plot but I am having a lot of trouble doing so.
I am able to make a forest plot with the following code:
###dataframe
###dataframe
library(ggplot2)
library(tidyr)
library(grid)
library(gridExtra)
library(forcats)
forestdf <- structure(list(labels = structure(1:36, .Label = c("Age*", "Sex – male vs. female",
"Body-mass index*,1 ", "Systolic blood pressure*", "Race - vs. white",
"Asian", "Black", "Townsend deprivation index", "Social habit",
"Smoking - vs. never", "Previous", "Current", "Alcohol use - vs. never",
"Once or twice a week", "Three or four times a week", "Daily or almost daily",
"Comorbidity", "Cancer", "Diabetes", "Chronic obstructive pulmonary disease2",
"Asthma", "Ischemic heart disease3", "Hypothyroidism", "Hypercholesterolemia",
"Allergic rhinitis", "Depression", "Serology", "White blood cell count",
"Red blood cell count", "Hemoglobin concentration", "Mean corpuscular volume",
"Mean corpuscular hemoglobin concentration", "Platelet count",
"Lymphocyte count", "Monocyte count", "Neutrophil count"), class = "factor"),
rr = c(1.18, 1.45, 1.76, 0.98, NA, 2.16, 2.65, 1.09, NA,
NA, 1.35, 1.15, NA, 0.73, 0.63, 0.63, NA, 1.23, 1.34, 1.51,
1.12, 1.46, 0.96, 1.1, 1.18, 1.38, NA, 1.03, 0.87, 0.93,
1, 0.94, 1, 1.03, 1.17, 1.06), rrhigh = c(1.08, 1.28, 1.57,
0.95, NA, 1.63, 2.03, 1.07, NA, NA, 1.18, 0.94, NA, 0.58,
0.49, 0.5, NA, 0.99, 1.08, 1.09, 0.93, 1.15, 0.71, 0.92,
0.91, 1.1, NA, 1.02, 0.73, 0.87, 0.99, 0.88, 1, 1.01, 1.03,
1.01), rrlow = c(1.28, 1.64, 1.97, 1.02, NA, 2.86, 3.44,
1.11, NA, NA, 1.55, 1.42, NA, 0.9, 0.79, 0.81, NA, 1.53,
1.66, 2.09, 1.34, 1.85, 1.3, 1.31, 1.52, 1.74, NA, 1.04,
1.03, 0.98, 1.01, 1.01, 1, 1.05, 1.32, 1.1)), class = "data.frame", row.names = c(NA,
-36L))
forestdf$labels <- factor(forestdf$labels,levels = forestdf$labels)
levels(forestdf$labels) 1.52, 1.74, NA, 1.04, 1.03, 0.98, 1.01, 1.01, 1, 1.05, 1.32,
#forestplot
p <- ggplot(forestdf, aes(x=rr, y=labels, xmin=rrlow, xmax=rrhigh))+
geom_pointrange(shape=22, fill="black")+
geom_vline(xintercept = 1, linetype=3)+
xlab("Variable")+ylab("Adjusted Relative Risk with 95% Confidence Interval")+theme_classic()+scale_y_discrete(limits = rev(labels))+
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))
p
However, I cannot get the left panel with labels to work:
#dataframe for table
fplottable <- structure(list(labels = structure(c(1L, 30L, 7L, 33L, 27L, 4L,
6L, 35L, 32L, 31L, 26L, 11L, 2L, 24L, 34L, 12L, 10L, 8L, 14L,
9L, 5L, 18L, 17L, 16L, 3L, 13L, 29L, 36L, 28L, 15L, 21L, 20L,
25L, 19L, 22L, 23L), .Label = c("Age*", "Alcohol use - vs. never",
"Allergic rhinitis", "Asian", "Asthma", "Black", "Body-mass index*,1 ",
"Cancer", "Chronic obstructive pulmonary disease2", "Comorbidity",
"Current", "Daily or almost daily", "Depression", "Diabetes",
"Hemoglobin concentration", "Hypercholesterolemia", "Hypothyroidism",
"Ischemic heart disease3", "Lymphocyte count", "Mean corpuscular hemoglobin concentration",
"Mean corpuscular volume", "Monocyte count", "Neutrophil count",
"Once or twice a week", "Platelet count", "Previous", "Race - vs. white",
"Red blood cell count", "Serology", "Sex – male vs. female",
"Smoking - vs. never", "Social habit", "Systolic blood pressure*",
"Three or four times a week", "Townsend deprivation index", "White blood cell count"
), class = "factor"), No..of.Events = c(1073L, 581L, 1061L, 1031L,
NA, 57L, 68L, 1072L, NA, NA, 442L, 117L, NA, 262L, 191L, 172L,
NA, 96L, 107L, 41L, 146L, 86L, 52L, 170L, 66L, 84L, NA, 1009L,
1009L, 1009L, 1009L, 1009L, 1009L, 1005L, 1005L, 1005L), ARR..95..CI. = c("1.18 (1.08-1.28)",
"1.45 (1.28-1.64)", "1.76 (1.57-1.97)", "0.98 (0.95-1.02)", "",
"2.16 (1.63-2.86)", "2.65 (2.03-3.44)", "1.09 (1.07-1.11)", "",
"", "1.35 (1.18-1.55)", "1.15 (0.94-1.42)", "", "0.73 (0.58-0.90)",
"0.63 (0.49-0.79)", "0.63 (0.50-0.81)", "", "1.23 (0.99-1.53)",
"1.34 (1.08-1.66)", "1.51 (1.09-2.09)", "1.12 (0.93-1.34)", "1.46 (1.15-1.85)",
"0.96 (0.71-1.30)", "1.10 (0.92-1.31)", "1.18 (0.91-1.52)", "1.38 (1.10-1.74)",
"", "1.03 (1.02-1.04)", "0.87 (0.73-1.03)", "0.93 (0.87-0.98)",
"1.00 (0.99-1.01)", "0.94 (0.88-1.01)", "1.00 (1.00-1.00)", "1.03 (1.01-1.05)",
"1.17 (1.03-1.32)", "1.06 (1.01-1.10)")), class = "data.frame", row.names = c(NA,
-36L))
###NOT WORKING CODE THAT TRIES TO MAKE TABLE LEFT OF FOREST PLOT
data_table <- geom_text(data=fplottable,aes(y=labels)) +
geom_text(label=eventnum) +
geom_text(label=arr)
data_table
grid.arrange(data_table,p, ncol=2)
I am drawing inspiration from:
Reproduce table and plot from journal and trying to get something similar to what is shown in the forest plot with the pink boxes
There were a few issues as #efz pointed out. In addition, you need to refactor the labels in your second column to allow them to match up with those in your first. It's probably going to look messy with the y axis labels and title alongside the table, so these could be removed too.
That leaves you something like:
forestdf$colour <- rep(c("white", "gray95"), 18)
p <- ggplot(forestdf, aes(x = rr, y = labels, xmin = rrlow, xmax = rrhigh)) +
geom_hline(aes(yintercept = labels, colour = colour), size = 7) +
geom_pointrange(shape = 22, fill = "black") +
geom_vline(xintercept = 1, linetype = 3) +
xlab("Variable") +
ylab("Adjusted Relative Risk with 95% Confidence Interval") +
theme_classic() +
scale_colour_identity() +
scale_y_discrete(limits = rev(forestdf$labels)) +
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())
names(fplottable) <- c("labels", "eventnum", "arr")
fplottable$labels <- factor(fplottable$labels, rev(levels(forestdf$labels)))
fplottable$colour <- rep(c("white", "gray95"), 18)
data_table <- ggplot(data = fplottable, aes(y = labels)) +
geom_hline(aes(yintercept = labels, colour = colour), size = 7) +
geom_text(aes(x = 0, label = labels), hjust = 0) +
geom_text(aes(x = 5, label = eventnum)) +
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)
You can simplify further by merging the two dataframes as fdf <- full_join(forestdf, fplottable, by = "labels") and running your p on fdf. Then p + geom_text(aes(x=22, label=paste(" ", arr," ",eventum, sep=' '))) will give the following output: output
Obviously, limits need to be expanded to 100 to include the table, and the full code is below:
p <- ggplot(fdf, aes(x=rr, y=labels, xmin=rrlow, xmax=rrhigh))+
geom_pointrange(shape=22, fill="black") +
geom_vline(xintercept = 1, linetype=3) +
xlab("Variable")+ylab("Adjusted Relative Risk with 95% Confidence Interval") +
theme_bw() +
#scale_y_discrete(limits = rev(labels))+
scale_x_log10(limits = c(0.25, 100),
breaks = c(0.25, 0.5, 1, 2, 4, 100),
labels=c("0.25", "0.5", "1", "2", "4", ""),
expand = c(0,0)
)+
geom_text(aes(x=22, label=paste(" ", arr," ",eventum, sep=' ')))
p
supposing
names(fplottable)<-c('labels','eventum','arr')
then there are a few issues with the code for data_table. If I understood correctly you meant something like:
data_table <- ggplot(data=fplottable)+geom_text(aes(x= 1, y=labels, label=arr))+geom_text(aes(x= 1.5, y=labels, label=eventum)).
You can play with the value of x and have only one geom_text where label=paste(arr, eventum, sep=' ')
in this case the command grid.arrange(data_table,p, ncol=2) seems to work fine. You can define the space of each panel with width.

col and row names in a Forest plot

I don't how to introduce columns and rows names into a forest plot. Here is the dataset:
structure(list(structure(c(5L, 6L, 4L, 3L, 2L, 1L), .Label = c("Austria",
"Denmark", "England", "France", "Portugal", "Spain"), class = "factor"),
Slope = c(-1.8511, -2.33305, -2.698, -1.46, -0.98, -1.401
), low = c(-3.3021, -4.305, -5.873, -4.86, -5.082, -2.887
), high = c(-0.4008, -0.364, -0.087, 2.98, 3.125, -0.054),
bS0 = c(-5.66, -2.709, -4.057, -1.224, -0.574, -1.43), Sf = c(0.354,
0.169, 0.253, 0.077, 0.036, 0.089), `S0-Sf` = c(0.146, 0.126,
0.169, 0.091, 0.061, 0.088)), .Names = c("", "Slope", "low",
"high", "bS0", "Sf", "S0-Sf"), class = "data.frame", row.names = c(NA,
-6L))
And here the script to get the Forest plot:
windows()
forestplot(labeltext=data, graph.pos=3,
mean=c(data$Slope),
lower=c(data$low), upper=c(data$high),
xlab="Regression Coefficient",
txt_gp=fpTxtGp(label=gpar(cex=1),
ticks=gpar(cex=1),
xlab=gpar(cex = 1),
title=gpar(cex = 1)),
col=fpColors(box="black", lines="black", zero = "gray50"),
zero=0, cex=0.9, lineheight = "auto", boxsize=0.1,
lwd.ci=1, ci.vertices=TRUE, ci.vertices.height = 0.1,colgap=unit(4,"mm"))
And here is the outcome:
However, I would like to introduce the names of the countries (column 1 of my data) and the columns names. How should I do it?
I don't know how to modify you solution, so I did it my way:
data <-
structure(list(
coef = c(NA,-1.85110, -2.33305, -2.69800, -1.46000, -0.98000, -1.40100),
low = c(NA,-3.3021, -4.3050, -5.8730, -4.8600, -5.0820, -2.8870),
high = c(NA,-0.4008, -0.3640, -0.0870, 2.9800, 3.1250, -0.0540)),
.Names = c("coef", "low", "high"),
row.names = c(NA, -7L),
class = "data.frame")
tabletext<-cbind(c(NA,"Portugal","Spain","France","Engald","Denmark","Austria"),
c("Slope", "-1.8511", "-2.33305", "-2.698", "-1.46", "-0.98", "-1.401"),
c("low", "-3.3021", "-4.305", "-5.873", "-4.86", "-5.082", "-2.887"),
c("high", "-0.4008", "-0.364", "-0.087", "2.98", "3.125", "-0.054"),
c("bS0", "-5.66", "-2.709", "-4.057", "-1.224", "-0.574", "-1.43"),
c("Sf", "-0.354", "0.169", "0.253", "0.077", "0.036", "0.089"),
c("S0-Sf", "0.146", "0.126", "0.169", "0.091", "0.061", "0.088"))
forestplot(tabletext,
data,new_page = TRUE,
clip=c(-6,3),
boxsize = .25,
graphwidth = unit(10, "cm"),
xlog=F,
col=fpColors(line="black"),
xlab="Regression Coefficient")
You need to use graph.pos argument inside the forestplot function.
For example, use graph.position=3 if you want the plot after the slope column

Resources