Resize your plot within R
ggplot2::ggsave
has reasonable defaults for image width and height that are equal to the size of the current graphic window. In my case it is the X11
device, which has a default value of 7x7 inches with 300 dpi that results in a 2100x2100 pixel png image. Obviously it is too large for web publishing or to display on a monitor, that typically has lower resolution.
There are several options to optimize your image. Most importantly you can always calibrate ggsave
arguments to your need. However, in some cases, it is more convenient to modify an original, high-quality image using external tools, such as graphics applications or using R’s dedicated packages to image post-processing. In this post I describe how I use Linux command line tools and magick
package image_resize
function.
Creating a basic plot with ggplot
First, I create and save an image with ggplot
using the well-known mtcars
dataset from ggplot2
.
pacman::p_load(ggplot2, magick) ## this will load (and install if required) libraries
si <- sessionInfo() ## Info on OS, obscure libraries (BLAS, LAPACK), R versions and attached packages
print(si, locale = FALSE) ## Omit irrelevant and boring locale info
## Plotting weight (in 1000 lbs), miles/(US) gallon and number of cylinders
p <- ggplot(mtcars, aes(wt, mpg, color = as.factor(cyl))) +
geom_point()
p
ggsave("p.png", p)
## Saving 7 x 7 in image
Getting to know the dimensions and size of saved image
Show saved file size and other info
file.size(pattern = "p.png") ## in 45511 B (byte)
file.info(pattern = "p.png") ## other file info (group, modiification time, etc)
You can always invoke a system command within an R session with system()
. To get basic info on saved image (p.png
) one can use file
command which prints dimensions (2100x2100) and or identify
that provides size in B (45338 B). Note that the latter program is a member of the ImageMagick suite of tools.
system(command = "file p.png")
## p.png: PNG image data, 2100 x 2100, 8-bit colormap, non-interlaced
system(command = "identify p.png")
## p.png PNG 2100x2100 2100x2100+0+0 8-bit sRGB 256c 45338B 0.000u 0:00.015
Other system tools available on most Linux distros:
exiv2
, a program to read and write Exif and other metadata, image comments.mediainfo
display detailed information on image on different output format like XML, JSON, HTML.
Resize using Magick
- R package
Magick
package is available from CRAN and it is based on Magick++ C++ graphics library. See details at project pages and package vignette
The following command throws:
## Error: The 'image' argument is not a magick image object.
image_resize(image = "p.png", geometry = "800x800")
First we should create an image object by reading in our saved image,
im1 <- image_read(path = "p.png")
than we can resize, view and save the new image:
im2 <- image_resize(image = im1, geometry = "800x800")
print(im2)
# X11 only
image_display(im2)
## System dependent
options(browser = "firefox") # choose whatever browser you like
image_browse(image = im2) # open in new browser tab
image_write(im2, path = "p_resized.png") # saving new image
system("xdg-open p_resized.png") # open with system default app like sxiv
Imagemagick (within R)
Use ImageMagick
system tools to convert, resize and manipulate images from command line.
Installation is simple, e.g. on Debian-based distros: sudo apt-get install imagemagick
, on Arch-like distros sudo pacman -S imagemagick
.
## running system command from imagemagick tool:
system("convert -geometry 800x p.png p_800x.png")
Appendix - Sessioninfo
si <- sessionInfo()
print(si, locale = FALSE)
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux
Matrix products: default
BLAS: /usr/lib/libopenblasp-r0.3.15.so
LAPACK: /usr/lib/liblapack.so.3.9.1
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] magick_2.7.2 ggplot2_3.3.3
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 magrittr_2.0.1 tidyselect_1.1.1 munsell_0.5.0
[5] colorspace_2.0-1 R6_2.5.0 rlang_0.4.11 fansi_0.5.0
[9] dplyr_1.0.6 tools_4.1.0 grid_4.1.0 gtable_0.3.0
[13] pacman_0.5.1 utf8_1.2.1 DBI_1.1.1 withr_2.4.2
[17] ellipsis_0.3.2 assertthat_0.2.1 tibble_3.1.2 lifecycle_1.0.0
[21] crayon_1.4.1 purrr_0.3.4 vctrs_0.3.8 glue_1.4.2
[25] compiler_4.1.0 pillar_1.6.1 generics_0.1.0 scales_1.1.1
[29] pkgconfig_2.0.3
>