Title: | Reveal a 'ggplot' Incrementally |
---|---|
Description: | Provides functions that make it easy to reveal 'ggplot2' graphs incrementally. The functions take a plot produced with 'ggplot2' and return a list of plots showing data incrementally by panels, layers, groups, the values in an axis or any arbitrary aesthetic. |
Authors: | Weverthon Machado [aut, cre, cph]
|
Maintainer: | Weverthon Machado <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.4 |
Built: | 2025-02-08 05:38:26 UTC |
Source: | https://github.com/weverthonmachado/ggreveal |
Turns a ggplot into a list of plots, showing data incrementally by an arbitrary aesthetic.
reveal_aes(p, aes = "group", order = NULL, max = 20)
reveal_aes(p, aes = "group", order = NULL, max = 20)
p |
A ggplot2 object |
aes |
which aesthetic to reveal E.g.: group, colour, shape, linetype |
order |
(optional) A numeric vector specifying in which order to reveal levels of the specified aesthetic. For example, if Any shape not included in the vector will be omitted from the incremental
plots. E.g.: with By default, the first plot is blank, showing layout elements (title,
legends, axes, etc) but no data. To omit the blank plot, include |
max |
maximum number of unique levels of aesthetic to be used |
A list of ggplot2 objects, which can be passed to reveal_save()
# Create full plot library(ggplot2) p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point(aes(shape=factor(am)), size=2) + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) p plot_list <- reveal_aes(p, "shape") plot_list[[1]] plot_list[[2]] plot_list[[3]] plot_list[[4]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
# Create full plot library(ggplot2) p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point(aes(shape=factor(am)), size=2) + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) p plot_list <- reveal_aes(p, "shape") plot_list[[1]] plot_list[[2]] plot_list[[3]] plot_list[[4]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
Turns a ggplot into a list of plots, showing data incrementally by groups.
Note that if the group
aesthetic is not explicitly defined in the original
plot, ggplot2
will set it to the interaction of all discrete variables (see
ggplot2::aes_group_order).
reveal_groups(p, order = NULL)
reveal_groups(p, order = NULL)
p |
A ggplot2 object |
order |
(optional) A numeric vector specifying in which order to reveal the groups For example, if there are three groups in the plot, Any group not included in the vector will be omitted from the incremental
plots. E.g.: with By default, the first plot is blank, showing layout elements (title,
legends, axes, etc) but no data. To omit the blank plot, include |
A list of ggplot2 objects, which can be passed to reveal_save()
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p plot_list <- reveal_groups(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p plot_list <- reveal_groups(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
Turns a ggplot into a list of plots, showing data incrementally by layers.
reveal_layers(p, order = NULL)
reveal_layers(p, order = NULL)
p |
A ggplot2 object |
order |
(optional) A numeric vector specifying in which order to reveal the layers For example, if there are three layers in the plot, Any layer not included in the vector will be omitted from the incremental
plots. E.g.: with By default, the first plot is blank, showing layout elements (title,
legends, axes, etc) but no data. To omit the blank plot, include |
A list of ggplot2 objects, which can be passed to reveal_save()
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p plot_list <- reveal_layers(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p plot_list <- reveal_layers(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
Turns a ggplot into a list of plots, showing data incrementally by panels.
reveal_panels(p, order = NULL, what = c("data", "everything"))
reveal_panels(p, order = NULL, what = c("data", "everything"))
p |
A ggplot2 object |
order |
(optional) A numeric vector specifying in which order to reveal the panels For example, if there are three panels in the plot, Any panel not included in the vector will be omitted from the incremental
plots. E.g.: with By default, the first plot is blank, showing layout elements (title,
legends, axes, etc) but no data. To omit the blank plot, include |
what |
(optional) one of |
A list of ggplot2 objects, which can be passed to reveal_save()
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p # Only data plot_list <- reveal_panels(p, what = "data") plot_list[[1]] plot_list[[2]] plot_list[[3]] # Everything plot_list <- reveal_panels(p, what = "everything") plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p # Only data plot_list <- reveal_panels(p, what = "data") plot_list[[1]] plot_list[[2]] plot_list[[3]] # Everything plot_list <- reveal_panels(p, what = "everything") plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
Saves incremental plots
reveal_save(plot_list, basename, ...)
reveal_save(plot_list, basename, ...)
plot_list |
A list of plots created by one of the |
basename |
The base file name that will be used for saving. |
... |
Additional arguments (e.g. width, height) to be passed to |
The paths of the saved plots, invisibly
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p plot_list <- reveal_groups(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(mpg, wt, color = factor(vs), group = factor(vs))) + geom_point() + geom_smooth(method="lm", formula = 'y ~ x', linewidth=1) + facet_wrap(~am) p plot_list <- reveal_groups(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
Turns a ggplot into a list of plots, showing data incrementally by the categories in the x or y axis.
reveal_x(p, order = NULL) reveal_y(p, order = NULL)
reveal_x(p, order = NULL) reveal_y(p, order = NULL)
p |
A ggplot2 object |
order |
(optional) A numeric vector specifying in which order to reveal the categories For example, if there are three categories in the axis, Any category not included in the vector will be omitted from the incremental
plots. E.g.: with By default, the first plot is blank, showing layout elements (title,
legends, axes, etc) but no data. To omit the blank plot, include |
A list of ggplot2 objects, which can be passed to reveal_save()
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(factor(vs), color = gear, fill= gear, group = gear)) + geom_bar() + facet_wrap(~am) p plot_list <- reveal_x(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))
# Create full plot library(ggplot2) data("mtcars") p <- mtcars |> ggplot(aes(factor(vs), color = gear, fill= gear, group = gear)) + geom_bar() + facet_wrap(~am) p plot_list <- reveal_x(p) plot_list[[1]] plot_list[[2]] plot_list[[3]] # Save plots reveal_save(plot_list, "myplot.png", width = 8, height = 4, path = tempdir()) # Clean temp files file.remove(list.files(path = tempdir(), pattern = "myplot", full.names = TRUE))