## ----include = FALSE---------------------------------------------------------- # R CMD check runs this code without knitr: pdf() can not draw Liberation Sans if (!isTRUE(getOption("knitr.in.progress"))) ragg::agg_capture() knitr::opts_chunk$set( collapse = TRUE, comment = "#>", dev = "ragg_png", dpi = 300, out.width = "100%", fig.width = 6, fig.height = 4 ) ## ----setup, message=FALSE----------------------------------------------------- library(munch) library(ggplot2) library(flextable) library(gdtools) fset <- font_set_liberation() set_theme(theme_minimal(base_family = "Liberation Sans", base_size = 11)) ## ----md-titles, message=FALSE------------------------------------------------- ggplot(mtcars, aes(mpg, wt)) + geom_point() + labs( title = "**Fuel Consumption** vs *Weight*", subtitle = "Data from the `mtcars` dataset", caption = "Source: *Motor Trend* (1974)" ) + theme( plot.title = element_md(size = 16), plot.subtitle = element_md(size = 12, code_font_family = "Liberation Mono"), plot.caption = element_md(size = 9) ) ## ----md-axes------------------------------------------------------------------ ggplot(mtcars, aes(hp, mpg)) + geom_point() + labs( x = "Horsepower (*hp*)", y = "Miles per gallon (**mpg**)" ) + theme( axis.title.x = element_md(), axis.title.y = element_md() ) ## ----long-title, message=FALSE, fig.height=3---------------------------------- long_title <- paste( "A **long title** that needs several lines, because it is wider", "than the *panel* of the plot" ) p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + labs(title = long_title) + theme(plot.title = element_md()) p ## ----long-title-patchwork, message=FALSE, fig.height=3------------------------ library(patchwork) p <- p + theme(plot.title = element_md(width = 2.5)) p | p ## ----md-ticks, message=FALSE, fig.height=3------------------------------------ species <- aggregate(Sepal.Length ~ Species, data = iris, FUN = mean) ggplot(species, aes(Species, Sepal.Length)) + geom_col(fill = "grey70") + scale_x_discrete( labels = c("*Iris setosa*", "*Iris versicolor*", "*Iris virginica*") ) + theme(axis.text.x = element_md(angle = 30, hjust = 1)) ## ----md-legend, message=FALSE, fig.height=3----------------------------------- ggplot(mtcars, aes(wt, mpg, colour = hp)) + geom_point() + labs(colour = "**Power**") + scale_colour_continuous(labels = function(x) paste0("**", x, "** hp")) + theme( legend.title = element_md(), legend.text = element_md() ) ## ----md-legend-lines, message=FALSE, fig.height=3----------------------------- cyl_labels <- c( "**4** cylinders", "**6** cylinders", "**8** cylinders \n*the most powerful*" ) p <- ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) + geom_point() + scale_colour_discrete(labels = cyl_labels) + theme(legend.text = element_md()) p ## ----md-legend-spacing, message=FALSE, fig.height=3--------------------------- p + theme( legend.key.height = unit(0.5, "lines"), legend.key.spacing.y = unit(4, "pt") ) ## ----md-all, message=FALSE, fig.height=3-------------------------------------- ggplot(mtcars, aes(wt, mpg, colour = factor(am))) + geom_point() + facet_wrap(~ paste0("**", cyl, "** cylinders")) + labs( title = "*Weight* and **consumption**", x = "Weight (*1000 lbs*)", y = "Miles per gallon", colour = "*Transmission*" ) + scale_colour_discrete(labels = c("**automatic**", "**manual**")) + theme(text = element_md()) ## ----chunks-overview, message=FALSE------------------------------------------- chunks <- as_paragraph( as_b("Bold"), as_chunk(", "), as_i("italic"), as_chunk(", "), as_chunk("x"), as_sup("2"), as_chunk(", "), as_chunk("H"), as_sub("2"), as_chunk("O") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme(plot.title = element_chunks(chunks)) ## ----stats-chunks, message=FALSE---------------------------------------------- title_chunks <- as_paragraph( as_chunk("Analysis: "), as_b("R"), as_sup("2"), as_chunk(" = 0.87, "), as_i("p"), as_chunk(" < 0.001") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_smooth(method = "lm", se = FALSE) + theme(plot.title = element_chunks(title_chunks)) ## ----chemical-chunks, message=FALSE------------------------------------------- subtitle_chunks <- as_paragraph( as_chunk("Concentration of "), as_b("H"), as_sub("2"), as_b("SO"), as_sub("4"), as_chunk(" in solution") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme(plot.subtitle = element_chunks(subtitle_chunks)) ## ----units-chunks, message=FALSE---------------------------------------------- caption_chunks <- as_paragraph( as_chunk("Units: mol\u00B7L"), as_sup("-1"), as_chunk(" | Speed: m\u00B7s"), as_sup("-2") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme(plot.caption = element_chunks(caption_chunks)) ## ----color-chunks, message=FALSE---------------------------------------------- title_chunks <- as_paragraph( as_chunk("Results: "), colorize(as_chunk("significant"), color = "#E63946"), as_chunk(" difference") ) subtitle_chunks <- as_paragraph( as_chunk("Compare "), as_highlight(as_chunk(" Group A "), color = "yellow"), as_chunk(" vs "), as_highlight(as_chunk(" Group B "), color = "cyan") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme( plot.title = element_chunks(title_chunks), plot.subtitle = element_chunks(subtitle_chunks) ) ## ----chunks-image, message=FALSE---------------------------------------------- r_logo <- file.path(R.home("doc"), "html", "logo.jpg") title_chunks <- as_paragraph( as_image(src = r_logo, width = 0.25, height = 0.2), as_chunk(" "), as_b("R Project"), as_chunk(" - fuel consumption data") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme(plot.title = element_chunks(title_chunks)) ## ----layout-chunks, message=FALSE--------------------------------------------- chunks <- as_paragraph( as_chunk("Centered: R"), as_sup("2"), as_chunk(" = 0.75") ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme(plot.title = element_chunks(chunks, hjust = 0.5)) ## ----geom-text-basic, message=FALSE------------------------------------------- annotations <- data.frame( x = c(15, 25, 30), y = c(4, 3, 2), label = c("**Group A**", "*Group B*", "`Group C`") ) ggplot(mtcars, aes(mpg, wt)) + geom_point(alpha = 0.5) + geom_text_md( data = annotations, aes(x = x, y = y, label = label), size = 4, code_font_family = "Liberation Mono" ) ## ----geom-label-example, message=FALSE---------------------------------------- ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_smooth(method = "lm", se = FALSE) + geom_label_md( label = "**R\u00B2 = 0.75**\n\n*p < 0.001*", x = 28, y = 5, hjust = 0, vjust = 1, fill = "white", label.padding = unit(0.5, "lines") ) ## ----geom-text-wrap, message=FALSE, fig.height=3------------------------------ df <- data.frame( x = c(15, 30), y = c(4, 2.5), label = c( "This is a **long annotation** that will be *wrapped* automatically when it exceeds the specified `width`.", "A shorter **note**." ) ) ggplot(mtcars, aes(mpg, wt)) + geom_point(alpha = 0.3) + geom_text_md( data = df, aes(x = x, y = y, label = label), width = 2, hjust = 0, size = 3.5, code_font_family = "Liberation Mono" ) ## ----geom-label-image, message=FALSE, fig.height=3---------------------------- r_logo <- file.path(R.home("doc"), "html", "logo.jpg") df <- data.frame( x = 20, y = 4, label = sprintf("Built with ![](%s) and **ggplot2**", r_logo) ) ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_label_md( data = df, aes(x = x, y = y, label = label), fill = "white", label.padding = unit(0.5, "lines") )