Reputation: 341
I am trying to make multiple facetted plots in r/ggplot2 that show R^2 and P values generated using the ggpmisc package, but format each line based on whether the P-value is below a certain number. I can successfully do this with a single character, the "R" and "P", but I cannot get the superscript 2 in R^2, the equal sign, or the value itself formatted. Possibly complicating things is that I'd like the value rounded to 3 decimal places, which I can do, but again I can't get the format to apply. Here is the problem code and output:
edit: added my.formula <- y ~ x
line of code at beginning of code to make it work.
library(tidyverse)
library(ggpmisc)
##edit: I left out this very important line previously (Thank you for pointing this out)
my.formula <- y ~ x
##data (sorry for all the extra whitespace, have never known how to remove this
ex1 <- structure(list(time = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
.Label = c("early", "late"), class = "factor"),
x = c(0.321386372587633, 0.321386372587633, 0.321386372587633, 0.321386372587633, 0.321386372587633,
0.344034242910647, 0.344034242910647, 0.344034242910647, 0.344034242910647, 0.344034242910647,
0.339242868568382, 0.339242868568382, 0.339242868568382, 0.339242868568382, 0.339242868568382,
0.319449901768173, 0.319449901768173, 0.319449901768173, 0.319449901768173, 0.319449901768173,
0.355824915824916, 0.355824915824916, 0.355824915824916, 0.355824915824916, 0.355824915824916,
0.343082264957265, 0.343082264957265, 0.343082264957265, 0.343082264957265, 0.343082264957265,
0.328739896647675, 0.328739896647675, 0.328739896647675, 0.328739896647675, 0.328739896647675,
0.321470937129300, 0.321470937129300, 0.321470937129300, 0.321470937129300, 0.321470937129300,
0.329134067099854, 0.329134067099854, 0.329134067099854, 0.329134067099854, 0.329134067099854,
0.303929221962009, 0.303929221962009, 0.303929221962009, 0.303929221962009, 0.303929221962009,
0.318415163880479, 0.318415163880479, 0.318415163880479, 0.318415163880479, 0.318415163880479,
0.299444516212376, 0.299444516212376, 0.299444516212376, 0.299444516212376, 0.299444516212376,
0.343325715822019, 0.343325715822019, 0.343325715822019, 0.343325715822019, 0.343325715822019,
0.372169617126390, 0.372169617126390, 0.372169617126390, 0.372169617126390, 0.372169617126390,
0.370415982484948, 0.370415982484948, 0.370415982484948, 0.370415982484948, 0.370415982484948,
0.356533513879486, 0.356533513879486, 0.356533513879486, 0.356533513879486, 0.356533513879486,
0.388973753645327, 0.388973753645327, 0.388973753645327, 0.388973753645327, 0.388973753645327,
0.372479078062834, 0.372479078062834, 0.372479078062834, 0.372479078062834, 0.372479078062834,
0.379030035822541, 0.379030035822541, 0.379030035822541, 0.379030035822541, 0.379030035822541,
0.407584269662921, 0.407584269662921, 0.407584269662921, 0.407584269662921, 0.407584269662921,
0.376392901361948, 0.376392901361948, 0.376392901361948, 0.376392901361948, 0.376392901361948,
0.317804974338729, 0.317804974338729, 0.317804974338729, 0.317804974338729, 0.317804974338729,
0.364539393114710, 0.364539393114710, 0.364539393114710, 0.364539393114710, 0.364539393114710,
0.379058888277380, 0.379058888277380, 0.379058888277380, 0.379058888277380, 0.379058888277380),
fctr = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L),
.Label = c("a", "b", "c", "d", "e"), class = "factor"),
y = c(4.04851970360232, -0.102591188819765, 3.73315302756709, 0.340504779534468, 0.237913590714702,
4.06911664322439, -0.0987598497016705, 5.54001741914177, 0.373135505872404, 0.274375656170733,
3.67443913261548, -0.0875837250365816, 3.03232358376749, 0.326682585279794,
0.239098860243213, 4.17944244767142, -0.0889017722380819, 3.1663639174688,
0.242471128656955, 0.153569356418873, 3.21516073180644, -0.0825729432584231,
6.99230459733604, 0.41716036686455, 0.334587423606127, 3.9367867766317,
-0.100174510844092, 2.72154053689335, 0.328889772105954, 0.228715261261863,
3.99729911731233, -0.100900126928578, 4.53860828306993, 0.366724076035605,
0.265823949107028, 4.56693109900323, -0.126308420437427, 5.10762234664757,
0.395180555800261, 0.268872135362834, 3.51448237912049, -0.0878426078144829,
5.65594606508526, 0.248915416023726, 0.161072808209243, 3.1383099374462,
-0.0732066492304829, 4.64242423111922, 0.217790427134848, 0.144583777904365,
4.49027171118563, -0.113362808190544, 4.80536464343379, 0.36523834263989,
0.251875534449346, 3.45497839018504, -0.0846503583222099, 3.45264289330118,
0.286441498422214, 0.201791140100004, 2.09894643097191, -0.0181292647706588,
3.15649251734621, 0.487077302298916, 0.468948037528257, 2.88011613789647,
0.0332011441108258, 2.17243045829905, 0.473011569737156, 0.506212713847982,
3.34598270139375, -0.0610926284972918, 2.64804765938524, 0.50849363186508,
0.447401003367788, 2.50787708448308, -0.0538689982930191, 2.38574021348553,
0.484438241081951, 0.430569242788932, 3.50259329310981, -0.0851862773159426,
3.14484623466867, 0.675500135099749, 0.590313857783806, 5.7188910696372,
-0.0954071517814848, 0.583392623483105, 0.624411329255129, 0.529004177473644,
2.67827579027081, 0.0460408230771886, 2.52145840963862, 0.625860271515617,
0.671901094592805, 3.13901517396219, -0.0693247901648161, 3.2356649048874,
0.668874437622921, 0.599549647458105, 2.51959640215471, -0.049164926875836,
2.23187740027734, 0.492702963511537, 0.443538036635701, 2.42625504206661,
0.0874001285858868, 2.8286583173362, 0.545165351011274, 0.632565479597161,
3.24857901035993, -0.0689851948115451, 2.91350545205866, 0.569549019107752,
0.500563824296207, 2.87505027703064, -0.0611185132088805, 3.2680427609413,
0.552874791715019, 0.491756278506139)), row.names = c(NA, -120L), class = "data.frame")
gtest <- ggplot(data = ex1, aes(x, y)) +
geom_hline(yintercept = -Inf, size = 0.6) +
geom_vline(xintercept = -Inf, size = 0.8) +
scale_x_continuous(expand = expansion(mult = c(.06, .1))) +
scale_y_continuous(expand = expansion(mult = c(.1, .6))) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, formula = my.formula) +
stat_poly_eq(formula = my.formula,
output.type = "expression", parse = TRUE,
aes(label = ifelse(stat(p.value) < 0.05,
paste("bold(R)^2 == ", round(stat(r.squared), digits = 3)), ## at least shows the r.squared value, but I can't get the rest bold
ifelse(stat(p.value) < 0.1,
paste("plain(R)^2 == ", round(stat(r.squared), digits = 3)), ##this contains plain(R) because the default is italic, which I don't want
""))),
size = 3,
label.x = .1,
label.y = .9) +
stat_poly_eq(formula = my.formula,
output.type = "expression", parse = TRUE,
aes(label = ifelse(stat(p.value) < 0.001,
paste("italic(P) < 0.001"),
ifelse(stat(p.value) < 0.05,
paste("bolditalic(P) == ", round(stat(p.value), digits = 3)), ##at least shows the p.value, but I can't get the rest bold
paste("italic(P) == ", round(stat(p.value), digits = 3))))),
size = 3,
label.x = .1,
label.y = .76) +
theme(axis.title.x = element_text(colour = "black", face = "bold", size = 14, margin = margin(10,0,0,0)),
axis.title.y = element_blank(),
legend.key = element_blank(),
legend.title = element_blank(),
plot.margin = unit(c(0.2,0.4,.2,.2), "cm"),
plot.title = element_text(colour = "black", face = "bold", size = 16, hjust = 0.5,margin = margin(0,0,10,0)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.spacing.x = unit(2, "pt"),
panel.spacing.y = unit(4, "pt"),
panel.background = element_blank(),
strip.placement = "outside",
strip.text.x = element_text(colour = "black", face = "bold", size = 12, margin = margin(0,0,6,0)),
strip.text.y = element_text(colour = "black", face = "bold", size = 12, margin = margin(0,3,0,0))) +
facet_grid(fctr ~ time, scales = "free", switch = "y")
gtest
I'm assuming that the paste()
in aes(label = ...)
is not base paste
but rather plotmath paste
, since output.type = "expression"
and parse = TRUE
in the stat_poly_eq
function call. Futhermore, I've seen numerous examples using bquote()
, mtext()
, substitute()
, etc., instead of paste()
, but I cannot get them to do what I'd like. Also, I don't think this has anything to do with the ggpmisc package, but rather my lack of understanding of how to format a string with a changing variable using plotmath expressions.
Here is what I generate with the code above. It's hard to see, but the R and P are in bold in panels (early, d) and (early, e), as they should be:
And here is what I'd like to see, with the entire line in bold:
Any help would be greatly appreciated!
Upvotes: 1
Views: 2354
Reputation: 6538
bolditalic()
and bold()
work like functions, applying the formatting to their argument. So, the code below should do what you want. I haven't tested this on your example as it does not run as is.
paste("bolditalic(P) == bold(\"", round(stat(p.value), digits = 3), "\")")
We need to enclose the number in quotation marks for bold()
to apply to it. These quotation marks need to be escaped as \"
so that they are recognized as part of the character string. R's expressions are so that numbers and Greek symbols are never typeset as bold. So, by including embedded quotation marks when the text is parsed the numbers are seen as character strings rather than numeric constants. So the key question here is how to format numbers as bold in R plotmath expressions.
Upvotes: 1