Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggbot #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

export(easy_remove_legend)
export(easy_rotate_x_labels)
export(ggbot)
import(ggplot2)
import(rlang)
69 changes: 69 additions & 0 deletions R/ggbot.R
Original file line number Diff line number Diff line change
@@ -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(...)), " "))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With multiple arguments, this mashes all the words together into one vector. Perhaps distinct string inputs should be considered separate commands? e.g. ggbot("rotate x axis", "remove y axis") is something that could supported, and the words in each of those commands would ideally be kept separate. Unless the idea is that there should only one command per ggbot call -- in which case combining all the words together makes sense.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case I had in mind was either one whole command or a vector of words. The use case you suggest is probably much more likely though; I should iterate over the elements of ... and treat each one as a call to ggbot.


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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if multiple "funs" are provided, maybe all should be removed? e.g. "remove legend x axis" should maybe remove both?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds reasonable. I went with just the first because I'm still thinking about contamination between requests, but I need to build some examples with multiple funs and see how that looks.


## 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())
}
}

}

31 changes: 31 additions & 0 deletions man/ggbot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions vignettes/shortcuts.R
Original file line number Diff line number Diff line change
@@ -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")

63 changes: 61 additions & 2 deletions vignettes/shortcuts.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
```


Loading