ggpreview {mlbplotR} | R Documentation |
Preview ggplot in Specified Dimensions
Description
This function previews a ggplot in its actual dimensions in order to see how it will look when saved. It is also significantly faster than the default preview in RStudio for ggplots created using mlbplotR.
Usage
ggpreview(
plot = ggplot2::last_plot(),
width = NA,
height = NA,
asp = NULL,
dpi = 300,
device = "png",
units = c("in", "cm", "mm", "px"),
scale = 1,
limitsize = TRUE,
bg = NULL,
...
)
Arguments
plot |
Plot to save, defaults to last plot displayed. |
width , height , units |
Plot size in |
asp |
The aspect ratio of the plot calculated as |
dpi |
Plot resolution. Also accepts a string input: "retina" (320), "print" (300), or "screen" (72). Applies only to raster output types. |
device |
Device to use. Can either be a device function (e.g. png), or one of "eps", "ps", "tex" (pictex), "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). |
scale |
Multiplicative scaling factor. |
limitsize |
When |
bg |
Background colour. If |
... |
Other arguments passed on to the graphics device function,
as specified by |
Value
No return value, called for side effects.
Examples
library(mlbplotR)
library(ggplot2)
team_abbr <- valid_team_names()
# remove league logos from this example
team_abbr <- team_abbr[!team_abbr %in% c("AL", "NL", "MLB")]
df <- data.frame(
random_value = runif(length(team_abbr), 0, 1),
teams = team_abbr
)
# use logos for x-axis
# note that the plot is assigned to the object "p"
p <- ggplot(df, aes(x = teams, y = random_value)) +
geom_col(aes(color = teams, fill = teams), width = 0.5) +
scale_color_mlb(type = "secondary") +
scale_fill_mlb(alpha = 0.4) +
theme_minimal() +
theme(axis.text.x = element_mlb_logo())
# preview p with defined width and aspect ratio (only available in RStudio)
if (rstudioapi::isAvailable()){
ggpreview(p, width = 5, asp = 16/9)
}