I have 3 tables that I'd like to be left-aligned. I swear I got this working yesterday, but all of a sudden it now returns an error (put it after the code)
Here's the data and code:
> dput(features)
structure(list(`2018` = c(4, 4, 4), `vs '17` = c(0, 1, 3), `vs Pilot` = c(0.47,
0.58, 0.26), `vs ALL` = c(0.37, 0.48, 0.22), `vs General` = c(0.42,
0.54, 0.21)), row.names = c(NA, -3L), class = "data.frame")
> dput(features_matrix)
structure(c("black", "black", "black", "black", "green", "green",
"green", "green", "green", "green", "green", "green", "green",
"green", "green"), .Dim = c(3L, 5L), .Dimnames = list(NULL, c("2018",
"vs '17", "vs Pilot", "vs ALL", "vs General"
)))
> dput(youth)
structure(list(`2018` = c(4, 3, 3, NaN, NaN, NaN), `vs '17` = c(0,
1, 1, NaN, NaN, NaN), `vs Pilot` = c(0.32, 0.63, 0.95,
NaN, NaN, NaN), `vs ALL` = c(0.26, 0.59, 0.93, NaN, NaN,
NaN), `vs General` = c(0.29, 0.46, 0.83, NaN, NaN, NaN)), row.names = c(NA,
-6L), class = "data.frame")
> dput(youth_matrix)
structure(c("black", "black", "black", "black", "black", "black",
"black", "green", "green", NA, NA, NA, "green", "red", "green",
NA, NA, NA, "green", "red", "green", NA, NA, NA, "green", "red",
"green", NA, NA, NA), .Dim = 6:5, .Dimnames = list(NULL, c("2018",
"vs '17", "vs BoSTEM Pilot", "vs BoSTEM ALL", "vs General SY"
)))
> dput(engage_diff)
structure(list(`2018` = c(4, 3, 4, NaN, NaN, NaN), `vs '17` = c(3,
1, 1, NaN, NaN, NaN), `vs Pilot` = c(1.32, -0.05, 1.21,
NaN, NaN, NaN), `vs ALL` = c(1.22, -0.19, 1.07, NaN, NaN,
NaN), `vs General` = c(1.21, -0.08, 1.17, NaN, NaN, NaN)), row.names = c(NA,
-6L), class = "data.frame")
> dput(engage_matrix)
structure(c("black", "black", "black", "black", "black", "black",
"green", "green", "green", NA, NA, NA, "green", "red", "green",
NA, NA, NA, "green", "red", "green", NA, NA, NA, "green", "red",
"green", NA, NA, NA), .Dim = 6:5, .Dimnames = list(NULL, c("2018",
"vs '17", "vs BoSTEM Pilot", "vs BoSTEM ALL", "vs General SY"
)))
comp_table_cols <- c("2018", "vs '17", "vs Pilot", "vs ALL", "vs SY")
tt <- ttheme_minimal(core=list(fg_params = list(col = features_matrix)),
colhead=list(fg_params=list(col="black", fontface=1L, cex = 0.8)),
rowhead=list(fg_params=list(col=NA))
)
features_diff_plot <- tableGrob(features_diff, theme = tt)
colnames(youth_diff) <- comp_table_cols
youth_matrix <- ifelse(engage_diff < 0, "red", ifelse(youth_diff > 0, "green", "black"))
youth_matrix[,1] <- "black"
tt <- ttheme_minimal(core=list(fg_params = list(col = youth_matrix)),
colhead=list(fg_params=list(col=NA)),
rowhead=list(fg_params=list(col=NA))
)
youth_diff_plot <- tableGrob(youth_diff, theme = tt)
colnames(engage_diff) <- comp_table_cols
engage_matrix <- ifelse(engage_diff < 0, "red", ifelse(engage_diff > 0, "green", "black"))
engage_matrix[,1] <- "black"
tt <- ttheme_minimal(core=list(fg_params = list(col = engage_matrix)),
colhead=list(fg_params=list(col=NA)),
rowhead=list(fg_params=list(col=NA))
)
engage_diff_plot <- tableGrob(engage_diff, theme = tt)
colnames(stem_diff) <- comp_table_cols
stem_matrix <- ifelse(stem_diff < 0, "red", ifelse(stem_diff > 0, "green", "black"))
stem_matrix[,1] <- "black"
tt <- ttheme_minimal(core=list(fg_params = list(col = stem_matrix)),
colhead=list(fg_params=list(col=NA)),
rowhead=list(fg_params=list(col=NA))
)
stem_diff_plot <- tableGrob(stem_diff, theme = tt)
plot_grid(features, youth, engage,
, ncol = 1
, rel_heights = c(3, 6, 8)
#, axis = "l"
, align = "h"
)
Error:
Warning message:
In align_plots(plotlist = plots, align = align, axis = axis) :
Graphs cannot be horizontally aligned, unless axis parameter set. Placing graphs unaligned.
Wish I could upload a picture of what the output should look like, but it should be so that all the columns just align up with each other.
I tried messing around with the axis and align parameters of the plot_grid function, but can't get it to plot these tables with the left-alignment. I don't get the error message when I uncomment the axis parameter, but it doesn't resolve the alignment issue. Any help would be appreciated
Related
I want to give names to individual starplots in R
stars(norm_datas[, 1:12], full = TRUE,radius = TRUE,len = 1.0, key.loc = c(14,1), labels = abbreviate(case.names(norm_datas)),main = "Provision of Ecosystem services", draw.segments = TRUE, lwd = 0.25, lty = par("lty"), xpd = TRUE).
This is what I tried but it just labeled each star plot as 1, 2, 3.
Kindly help resolve.
structure(list(Type_Garden = c("AG", "AG", "AG"), Pollinators = c(10,
6, 5.5), Flower_abundance = c(384, 435, 499), Climate_regulation = c(1,
7, 2), Crop_area = c(34, 25, 10), Plant_diversity = c(22, 53,
41), Nitrogen_balance = c(0.95, 0.26, NA), Phosphorus_balance = c(0.24,
0.04, NA), Habitat_provision = c(1, 2, 0), Recreation_covid = c(1,
NA, NA), Aesthetic_appreciation = c(3, NA, NA), Reconnection_nature = c(4,
NA, NA), Mental_health = c(1, NA, NA), Physical_health = c(1,
NA, NA)), class = "data.frame", row.names = c(NA, -3L))
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:
Using the forestplot package in the programming language R, I would like to make a forest plot that has each row in a different color. By each row, I mean the boxes and the respective confidence intervals.
Taking an example from the vignette [https://cran.r-project.org/web/packages/forestplot/vignettes/forestplot.html],
library(forestplot)
# Cochrane data from the 'rmeta'-package
cochrane_from_rmeta <-
structure(list(
mean = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531),
lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA, 0.731)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -11L),
class = "data.frame")
tabletext <- cbind(c("", "Study", "Auckland", "Block", "Doran", "Gamsu", "Morrison", "Papageorgiou", "Tauesch", NA, "Summary"),
c("Deaths", "(steroid)", "36", "1", "4", "14", "3", "1", "8", NA, NA),
c("Deaths", "(placebo)", "60", "5", "11", "20", "7", "7", "10", NA, NA),
c("", "OR", "0.58", "0.16", "0.25", "0.70", "0.35", "0.14", "1.02", NA, "0.53"))
forestplot(tabletext, cochrane_from_rmeta, new_page = TRUE,
is.summary = c(TRUE, TRUE, rep(FALSE, 8), TRUE),
clip = c(0.1,2.5), xlog = TRUE,
col = fpColors(box="royalblue",line="darkblue", summary="royalblue"))
I want each study to have its own color (Auckland can be colored blue, Block can be colored red, Doran can be colored green, and so on). I think that this might be accomplished by changing the argument to the fpColors() function.
Is there any way to do this?
Take a look at fpShapesGp. With this, it's possible to color the rows in different colors.
An simple example for your code:
styles <- fpShapesGp(
lines = list(
gpar(col = "black"),
gpar(col = "blue"),
gpar(col = "black"),
gpar(col = "blue"),
gpar(col = "black"),
gpar(col = "blue"),
gpar(col = "black"),
gpar(col = "blue"),
gpar(col = "black"),
gpar(col = "blue"),
gpar(col = "black")
),
box = list(
gpar(fill = "black"),
gpar(fill = "blue"),
gpar(fill = "black"),
gpar(fill = "blue"),
gpar(fill = "black"),
gpar(fill = "blue"),
gpar(fill = "black"),
gpar(fill = "blue"),
gpar(fill = "black"),
gpar(fill = "blue"),
gpar(fill = "black")
)
)
forestplot(tabletext, cochrane_from_rmeta, new_page = TRUE,
is.summary = c(TRUE, TRUE, rep(FALSE, 8), TRUE),
clip = c(0.1,2.5), xlog = TRUE,
shapes_gp = styles)
As a result you get this:
I am creating a forestplot using the forestplot package in R, and am having trouble with a few things.
Questions:
Is it possible to merge two adjacent text elements
Is it possible to modify either a single text element font, or the font of an entire row
My Code:
library(forestplot)
# creating text
text <- rbind(c('', 'N (%)', 'SRT', 'ART', 'HR [95% CI]'),
c('', '', '5 year survival %', '5 year survival %', ''),
c('Seminal Vesicle Involvement', '', '', '', ''),
c(' Yes', '10 (20%)', '94', '12', '0.73 [0.36, 1.50]'),
c(' No', '40 (80%)', '96', '10', '1.78 [0.73, 4.35]'),
c('Gender', '', '', '', ''),
c(' Male', '13 (22.5%)', '84', '22', '0.06 [-0.2, 0.86]'),
c(' Female', '37 (77.5%)', '93', '13', '1.89 [0.90, 6.67]'))
# creating the plot
forestplot(text,
mean = c(NA, NA, NA, 0.73, 1.78, NA, 0.06, 1.89),
lower = c(NA, NA, NA, 0.36, 0.73, NA, -0.2, 0.90),
upper = c(NA, NA, NA, 1.50, 4.35, NA, 0.86, 6.67),
is.summary=c(T, T, T, F, F, T, F, F),
lineheight = unit(0.9, "cm"),
graph.pos = 5,
graphwidth = unit(4, 'cm'),
xticks = c(-1, 0, 1, 2, 3, 4),
ci.vertices = T,
txt_gp = fpTxtGp(ticks = gpar(cex = 1),
xlab = gpar(cex = 1),
label = gpar(cex = 0.8),
summary = gpar(cex = 0.8)),
col=fpColors(box="black",
line="darkgrey",
summary="black",
zero='grey20',
axes='grey20'),
hrzl_lines = list("2" = gpar(lwd=1, col = "#000044")))
Output:
Desired:
I would like the two 5 year survival % text bits to be combined into 1 (and centered between the two headings above), and either just those elements or the whole row to be italic font.
I have tried using summary=list(gpar(...)) for the txt_gp option, but that only seems to be able to modify the whole column, and I have found nothing on merging cells at all.
If you make the colgap much smaller in forestplot than usual, you can split the text that is currently duplicated in row 2 in columns 3 and 4 into two parts:
> text[2, 4] <- 'survival % '
> text[2, 3] <- '5 year '
>
> forestplot(text,
+ mean = c(NA, NA, NA, 0.73, 1.78, NA, 0.06, 1.89),
+ lower = c(NA, NA, NA, 0.36, 0.73, NA, -0.2, 0.90),
+ upper = c(NA, NA, NA, 1.50, 4.35, NA, 0.86, 6.67),
+ is.summary=c(T, T, T, F, F, T, F, F),
+ lineheight = unit(0.9, "cm"),
+ graph.pos = 5,
+ graphwidth = unit(4, 'cm'),
+ xticks = c(-1, 0, 1, 2, 3, 4),
+ ci.vertices = T,
# add line---------
colgap=unit(.0011,"npc"),
#
+ txt_gp = fpTxtGp(ticks = gpar(cex = 1),
+ xlab = gpar(cex = 1),
+ label = gpar(cex = 0.8),
+ summary = gpar(cex = 0.8)),
+ col=fpColors(box="black",
+ line="darkgrey",
+ summary="black",
+ zero='grey20',
+ axes='grey20'),
+ hrzl_lines = list("2" = gpar(lwd=1, col = "#000044")))
I have an R dataframe called wSubset. I give the dput of this at the bottom of the question. I can generate a candlestick plot with the below line of code without problems:
if(!require("ggplot2")) { install.packages("ggplot2"); require("ggplot2") }
g <- ggplot(data=wSubset, aes(x=1:nrow(wSubset), lower=wSubset$candleLower, middle=wSubset$candleMiddle,
upper=wSubset$candleUpper, ymin=wSubset$low, ymax=wSubset$high)) +
geom_boxplot(stat='identity', aes(group=datetime, fill=fill))
Now, within wSubset$cluster I also store class information ranging from 1 to 5. I want to super-impose a geom_ribbon on top of the above g to colour-label different regions.
# the dataframe to be used by the geom_ribbon for colour-labelling
df_bg2 <- data.frame(x = c(0, rep(which(as.logical(diff(wSubset$cluster))), each=2), length(wSubset$cluster)),
ymin = min(scale(wSubset$low), na.rm = TRUE),
ymax = 1.1*max(scale(wSubset$high), na.rm = TRUE),
fill = factor(rep(wSubset$cluster[c(which(as.logical(diff(wSubset$cluster))),
length(wSubset$cluster) )], each=2)),
grp = factor(rep(seq(sum(as.logical(diff(wSubset$cluster)), na.rm=TRUE)+1), each=2))
)
# new plot which has candlesticks and the geom_ribbon colour-labelling
g2 <- ggplot(data=wSubset, aes(x=1:nrow(wSubset), lower=wSubset$candleLower, middle=wSubset$candleMiddle,
upper=wSubset$candleUpper, ymin=wSubset$low, ymax=wSubset$high)) +
geom_boxplot(stat='identity', aes(group=datetime, fill=fill)) +
geom_ribbon(data = df_bg2, aes(x = x, ymin=ymin, ymax=ymax, fill=fill, group=grp), alpha=.2) +
xlab("Date-Time") +
ylab("Levels") +
labs(title = "States in Temporal Display")
Here df_bg2 is used for filling the geom ribbons and works ok when I use geom_line, however I get the below error when I try to mix geom_boxplot with geom_ribbon.
Error: Aesthetics must either be length one, or the same length as the
dataProblems:wSubset$candleLower, wSubset$candleMiddle,
wSubset$candleUpper
Any idea where I am going wrong? This question is linked to an earlier question of mine on how to use geom_ribbon.
> dput(wSubset)
structure(list(date = structure(c(16342, 16342, 16342, 16342,
16342, 16342, 16343, 16343, 16343, 16343, 16343, 16343, 16344,
16344, 16344, 16344, 16344, 16344, 16345, 16345, 16345, 16345,
16345, 16345, 16346, 16346, 16346, 16346, 16346, 16346), class = "Date"),
datetime = structure(c(1411945200, 1411959600, 1411974000,
1411988400, 1412002800, 1412017200, 1412031600, 1412046000,
1412060400, 1412074800, 1412089200, 1412103600, 1412118000,
1412132400, 1412146800, 1412161200, 1412175600, 1412190000,
1412204400, 1412218800, 1412233200, 1412247600, 1412262000,
1412276400, 1412290800, 1412305200, 1412319600, 1412334000,
1412348400, 1412362800), class = c("POSIXct", "POSIXt"), tzone = ""),
open = c(1.62383, 1.62398, 1.62182, 1.62289, 1.62408, 1.62449,
1.6242, 1.62363, 1.62573, 1.62001, 1.62161, 1.62084, 1.62149,
1.61949, 1.61978, 1.62036, 1.62253, 1.61767, 1.61825, 1.61978,
1.62157, 1.61697, 1.61439, 1.6143, 1.61496, 1.61294, 1.61403,
1.60669, 1.59768, 1.59739), high = c(1.62517, 1.62426, 1.62618,
1.62559, 1.62745, 1.62522, 1.6245, 1.62678, 1.62873, 1.62253,
1.62297, 1.62175, 1.62194, 1.62018, 1.6214, 1.62261, 1.62521,
1.61873, 1.61999, 1.625, 1.62248, 1.61756, 1.615, 1.61624,
1.61591, 1.61405, 1.61403, 1.60844, 1.598, 1.5978), low = c(1.62258,
1.62166, 1.62143, 1.62162, 1.6233, 1.62375, 1.62247, 1.62361,
1.61936, 1.61664, 1.62026, 1.61998, 1.61886, 1.61792, 1.61618,
1.61798, 1.61739, 1.61724, 1.61817, 1.61962, 1.61561, 1.61283,
1.61126, 1.61358, 1.6128, 1.61248, 1.60665, 1.59769, 1.59518,
1.59594), close = c(1.62401, 1.62184, 1.6229, 1.62412, 1.62448,
1.62422, 1.62365, 1.62575, 1.62002, 1.6216, 1.62085, 1.62149,
1.61948, 1.61979, 1.62036, 1.62256, 1.61769, 1.61826, 1.61978,
1.62153, 1.61698, 1.61435, 1.61429, 1.61496, 1.61294, 1.61405,
1.60669, 1.59769, 1.5974, 1.59672), candleLower = c(1.62383,
1.62184, 1.62182, 1.62289, 1.62408, 1.62422, 1.62365, 1.62363,
1.62002, 1.62001, 1.62085, 1.62084, 1.61948, 1.61949, 1.61978,
1.62036, 1.61769, 1.61767, 1.61825, 1.61978, 1.61698, 1.61435,
1.61429, 1.6143, 1.61294, 1.61294, 1.60669, 1.59769, 1.5974,
1.59672), candleMiddle = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), candleUpper = c(1.62401, 1.62398,
1.6229, 1.62412, 1.62448, 1.62449, 1.6242, 1.62575, 1.62573,
1.6216, 1.62161, 1.62149, 1.62149, 1.61979, 1.62036, 1.62256,
1.62253, 1.61826, 1.61978, 1.62153, 1.62157, 1.61697, 1.61439,
1.61496, 1.61496, 1.61405, 1.61403, 1.60669, 1.59768, 1.59739
), fill = c("white", "red", "white", "white", "white", "red",
"red", "white", "red", "white", "red", "white", "red", "white",
"white", "white", "red", "white", "white", "white", "red",
"red", "red", "white", "red", "white", "red", "red", "red",
"red"), cluster = c(5, 4, 2, 2, 2, 5, 5, 2, 4, 2, 5, 5, 4,
5, 4, 2, 4, 1, 1, 2, 4, 4, 4, 5, 4, 5, 3, 3, 5, 5)), .Names = c("date",
"datetime", "open", "high", "low", "close", "candleLower", "candleMiddle",
"candleUpper", "fill", "cluster"), row.names = c(NA, 30L), class = "data.frame")
You want to do something like this:
wSubset <- within(wSubset, x<-1:nrow(wSubset))
g2 <- ggplot() +
geom_boxplot(stat='identity', data=wSubset, aes(x=x, lower=candleLower, middle=candleMiddle, upper=candleUpper,
group=datetime, fill=fill, ymin=low, ymax=high)) +
geom_ribbon(data = df_bg2, aes(x = x, ymin=ymin, ymax=ymax, fill=fill, group=grp), alpha=.2) +
xlab("Date-Time") +
ylab("Levels") +
labs(title = "States in Temporal Display")
Namely, separate out the global aesthetics to the geom_boxplot layer. Although, after looking at the plot, it would appear you need to adjust your values in df_bg2.