-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
base: master
Are you sure you want to change the base?
ggbot #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
export(easy_remove_legend) | ||
export(easy_rotate_x_labels) | ||
export(ggbot) | ||
import(ggplot2) | ||
import(rlang) |
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(...)), " ")) | ||
|
||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
} | ||
} | ||
|
||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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") | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 toggbot
.