diff --git a/NAMESPACE b/NAMESPACE index e7faf1a..5516749 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,5 +2,6 @@ export(easy_remove_legend) export(easy_rotate_x_labels) +export(ggbot) import(ggplot2) import(rlang) diff --git a/R/ggbot.R b/R/ggbot.R new file mode 100644 index 0000000..c12030e --- /dev/null +++ b/R/ggbot.R @@ -0,0 +1,69 @@ +#' Helper bot to change plot aesthetics +#' +#' @md +#' @param ... character vector containing lexical tokens (a command or a vector or words) +#' +#' @details Commands currently supported: +#' - change colour of points +#' - remove axis, legend, everything +#' +#' @return a \code{\link[ggplot2]{theme}} object which can be used in +#' \code{\link[ggplot2]{ggplot2}} calls. +#' @export +#' +#' @examples +#' library(ggplot2) +#' ggplot(mtcars, aes(cyl, hp, col = "red")) + +#' geom_point() + +#' ggbot("blue points") +ggbot <- function(...) { + + tokens <- unlist(strsplit(unlist(rlang::exprs(...)), " ")) + + all_colours <- colours() + all_aes <- ggplot2:::.all_aesthetics + all_funs <- c("theme", "axis", "legend", "points") + all_remove_cmds <- c("no", "remove", "drop", "clear") + + detect_cols <- na.omit(all_colours[pmatch(tokens, all_colours)]) + detect_aes <- na.omit(all_aes[pmatch(tokens, all_aes)]) + detect_funs <- na.omit(all_funs[pmatch(tokens, all_funs)]) + detect_remove_cmds <- na.omit(all_remove_cmds[pmatch(tokens, all_remove_cmds)]) + + ## change colours + if (length(detect_cols) > 0 && "points" %in% detect_funs) { + return(ggplot2::scale_colour_manual(values = detect_cols, labels = detect_cols)) + } + + ## remove components + if (length(detect_remove_cmds) > 0 && length(detect_funs) > 0) { + + ## remove theme + if (detect_funs[1] == "theme") return(theme_void()) + + ## remove axes + if (detect_funs[1] == "axis" && detect_aes[1] == "x") { + return(theme(axis.title.x = element_blank(), + axis.text.x = element_blank(), + axis.ticks.x = element_blank())) + } + if (detect_funs[1] == "axis" && detect_aes[1] == "y") { + return(theme(axis.title.y = element_blank(), + axis.text.y = element_blank(), + axis.ticks.y = element_blank())) + } + if (detect_funs[1] == "axis") { + return(theme(axis.title = element_blank(), + axis.text = element_blank(), + axis.ticks = element_blank())) + } + + ## remove legend(s) + if (detect_funs[1] == "legend") { + if (length(detect_aes) > 0) return(easy_remove_legend(detect_aes)) + return(easy_remove_legend()) + } + } + +} + diff --git a/man/ggbot.Rd b/man/ggbot.Rd new file mode 100644 index 0000000..50afebe --- /dev/null +++ b/man/ggbot.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ggbot.R +\name{ggbot} +\alias{ggbot} +\title{Helper bot to change plot aesthetics} +\usage{ +ggbot(...) +} +\arguments{ +\item{...}{character vector containing lexical tokens (a command or a vector or words)} +} +\value{ +a \code{\link[ggplot2]{theme}} object which can be used in +\code{\link[ggplot2]{ggplot2}} calls. +} +\description{ +Helper bot to change plot aesthetics +} +\details{ +Commands currently supported: +\itemize{ +\item change colour of points +\item remove axis, legend, everything +} +} +\examples{ +library(ggplot2) +ggplot(mtcars, aes(cyl, hp, col = "red")) + + geom_point() + + ggbot("blue points") +} diff --git a/vignettes/shortcuts.R b/vignettes/shortcuts.R new file mode 100644 index 0000000..c9a84af --- /dev/null +++ b/vignettes/shortcuts.R @@ -0,0 +1,90 @@ +## ---- message = FALSE---------------------------------------------------- +library(ggplot2) +library(cowplot) +library(ggeasy) + +p <- ggplot(mtcars, aes(hp, mpg)) + geom_point() + +## ---- fig.width = 6, fig.height = 6-------------------------------------- +p + labs(title = "ggplot2 default") + +## ---- fig.width = 8, fig.height = 8-------------------------------------- +p1 <- p + + easy_rotate_x_labels() + + labs(title = "default rotation") +p2 <- p + + easy_rotate_x_labels(angle = 45, side = "right") + + labs(title = "angle = 45") +p3 <- p + + easy_rotate_x_labels("startattop") + + labs(title = "text starts at top") +p4 <- p + + easy_rotate_x_labels("startatbottom") + + labs(title = "text starts at bottom") + +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") + +## ---- fig.width = 8, fig.height = 8-------------------------------------- +p <- ggplot(mtcars, aes(wt, mpg, colour = cyl, size = hp)) + + geom_point() + +p1 <- p + + labs(title = "With all legends") +p2 <- p + + easy_remove_legend() + + labs(title = "Remove all legends") +p3 <- p + + easy_remove_legend(size) + + labs(title = "Remove size legend") +p4 <- p + + easy_remove_legend(size, color) + + labs(title = "Remove both legends specifically") + +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") + +## ---- fig.width = 8, fig.height = 8-------------------------------------- +p <- ggplot(mtcars, aes(cyl, hp, col = "red")) + geom_point() + +p1 <- p + + labs(title = "Default red points") +p2 <- p + + ggbot("blue points") + + labs(title = "A string request") +p3 <- p + + ggbot("points", "blue") + + labs(title = "The order is irrelevant") +p4 <- p + + ggbot("blue", "point") + + labs(title = "Partial match to words") + +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") + +## ---- fig.width = 5, fig.height = 5-------------------------------------- +ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), size = hp)) + + geom_point() + + theme_dark() + + ggbot("make points red white and blue") + +## ---- fig.width = 8, fig.height = 8-------------------------------------- +p <- ggplot(mtcars, aes(cyl, hp, col = "red")) + geom_point() + +p1 <- p + + labs(title = "Default red points") +p2 <- p + + ggbot("drop x axis") + + labs(title = "No x axis") +p3 <- p + + ggbot("y axis remove") + + labs(title = "No y axis") +p4 <- p + + ggbot("remove legend") + + labs(title = "No legend") + +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") + +## ---- fig.width = 5, fig.height = 5-------------------------------------- +ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), size = hp)) + + geom_point() + + ggbot("I don't need no theme") + + ggbot("I don't need no legend") + diff --git a/vignettes/shortcuts.Rmd b/vignettes/shortcuts.Rmd index 5f0ab58..699d6a5 100644 --- a/vignettes/shortcuts.Rmd +++ b/vignettes/shortcuts.Rmd @@ -41,7 +41,7 @@ p4 <- p + easy_rotate_x_labels("startatbottom") + labs(title = "text starts at bottom") -plot_grid(p1, p2, p3, p4, nrow = 2) +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") ``` ## Removing legends @@ -64,8 +64,67 @@ p4 <- p + easy_remove_legend(size, color) + labs(title = "Remove both legends specifically") -plot_grid(p1, p2, p3, p4, nrow = 2) +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") ``` +## The `ggbot` + +Sometimes you don't even remember these helper functions. `ggbot` takes your commands and tries to interpret them in a way that modifies a plot. + +```{r, fig.width = 8, fig.height = 8} +p <- ggplot(mtcars, aes(cyl, hp, col = "red")) + geom_point() + +p1 <- p + + labs(title = "Default red points") +p2 <- p + + ggbot("blue points") + + labs(title = "A string request") +p3 <- p + + ggbot("points", "blue") + + labs(title = "The order is irrelevant") +p4 <- p + + ggbot("blue", "point") + + labs(title = "Partial match to words") + +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") +``` + +Various combinations of aesthetics can be requested, for example we can ask to change all colours of points + +```{r, fig.width = 5, fig.height = 5} +ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), size = hp)) + + geom_point() + + theme_dark() + + ggbot("make points red white and blue") +``` + +`ggbot` can also be used to remove components easily + +```{r, fig.width = 8, fig.height = 8} +p <- ggplot(mtcars, aes(cyl, hp, col = "red")) + geom_point() + +p1 <- p + + labs(title = "Default red points") +p2 <- p + + ggbot("drop x axis") + + labs(title = "No x axis") +p3 <- p + + ggbot("y axis remove") + + labs(title = "No y axis") +p4 <- p + + ggbot("remove legend") + + labs(title = "No legend") + +plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l") +``` + +or remove everything `theme`-related + +```{r, fig.width = 5, fig.height = 5} +ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), size = hp)) + + geom_point() + + ggbot("I don't need no theme") + + ggbot("I don't need no legend") +``` diff --git a/vignettes/shortcuts.html b/vignettes/shortcuts.html new file mode 100644 index 0000000..4212183 --- /dev/null +++ b/vignettes/shortcuts.html @@ -0,0 +1,186 @@ + + + + + + + + + + + + + + + + +ggplot2 Shortcuts + + + + + + + + + + + + + + + + + +

ggplot2 Shortcuts

+

Jonathan Carroll

+

2017-11-11

+ + + +

This package allows easy access to some common ggplot2 tasks. For example, rotating the x axis labels is a very frequently looked up task, and we can make it easier. If we create a simple ggplot2 plot

+
library(ggplot2)
+library(cowplot)
+library(ggeasy)
+
+p <- ggplot(mtcars, aes(hp, mpg)) + geom_point()
+

then by default, this looks like

+
p + labs(title = "ggplot2 default")
+

+

We can perform various rotations though

+
p1 <- p + 
+    easy_rotate_x_labels() + 
+    labs(title = "default rotation")
+p2 <- p + 
+    easy_rotate_x_labels(angle = 45, side = "right") + 
+    labs(title = "angle = 45")
+p3 <- p + 
+    easy_rotate_x_labels("startattop") + 
+    labs(title = "text starts at top")
+p4 <- p + 
+    easy_rotate_x_labels("startatbottom") + 
+    labs(title = "text starts at bottom")
+    
+plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l")
+

+
+

Removing legends

+

Removing legends is made easier by the easy_remove_legend function. When called without arguments, all legends are removed (equivalent to theme(legend.position = "none")). Alternatively, the names of aesthetics for which legends should be removed can be passed.

+
p <- ggplot(mtcars, aes(wt, mpg, colour = cyl, size = hp)) +
+    geom_point()
+
+p1 <- p + 
+    labs(title = "With all legends")
+p2 <- p + 
+    easy_remove_legend() + 
+    labs(title = "Remove all legends")
+p3 <- p + 
+    easy_remove_legend(size) + 
+    labs(title = "Remove size legend")
+p4 <- p + 
+    easy_remove_legend(size, color) + 
+    labs(title = "Remove both legends specifically")
+
+plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l")
+

+
+
+

The ggbot

+

Sometimes you don’t even remember these helper functions. ggbot takes your commands and tries to interpret them in a way that modifies a plot.

+
p <- ggplot(mtcars, aes(cyl, hp, col = "red")) + geom_point()
+
+p1 <- p + 
+    labs(title = "Default red points")
+p2 <- p + 
+    ggbot("blue points") + 
+    labs(title = "A string request")
+p3 <- p + 
+    ggbot("points", "blue") +
+    labs(title = "The order is irrelevant")
+p4 <- p + 
+    ggbot("blue", "point") +
+    labs(title = "Partial match to words")
+    
+plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l")
+

+

Various combinations of aesthetics can be requested, for example we can ask to change all colours of points

+
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), size = hp)) + 
+    geom_point() + 
+    theme_dark() + 
+    ggbot("make points red white and blue")
+

+

ggbot can also be used to remove components easily

+
p <- ggplot(mtcars, aes(cyl, hp, col = "red")) + geom_point()
+
+p1 <- p + 
+    labs(title = "Default red points")
+p2 <- p + 
+    ggbot("drop x axis") + 
+    labs(title = "No x axis")
+p3 <- p + 
+    ggbot("y axis remove") +
+    labs(title = "No y axis")
+p4 <- p + 
+    ggbot("remove legend") +
+    labs(title = "No legend")
+    
+plot_grid(p1, p2, p3, p4, nrow = 2, align = "hv", axis = "l")
+

+

or remove everything theme-related

+
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), size = hp)) + 
+    geom_point() + 
+    ggbot("I don't need no theme") +
+    ggbot("I don't need no legend")
+

+
+ + + + + + + +