ggsave is ignoring my specification for height and width

50 Views Asked by At

I am trying to save a ggplot graph for a publication with specific criteria. Whenever I go about saving the plot using ggsave setting the height, width, units and DPI I get an image that is always larger.

I went and tried with another dataset, the iris dataset, and I still have the same issue. Here is my code below.

iris %>%
  group_by(Species) %>%
  summarise(Mean = mean(Sepal.Length)) %>%
  ggplot(aes(x = Species,y = Mean)) +
  geom_bar(stat = "identity",color = "black",fill = "lightgrey") 
ggsave("Example.jpeg",height = 3,width = 5,units = "in",dpi = 600)

This is what the image size says in word after I insert the image. I would have expected the original size to be 3 inches tall and 5 inches wide. Saved Graph in Word

1

There are 1 best solutions below

0
GRowInG On

If you specify in ggplot2 a 3 by 5 inch (height x width) image with 600 dpi, the resulting jpeg-file will have 1800x3000 pixels. I crossvalidated your reprex using a png-file as requested output image (see below).

library (magick)

img <- image_read("Example.png")
image_attributes(img)                  

                   property                               value
1                date:create                     2024-03-08T10:48:06+00:00
2                date:modify                     2024-03-08T10:48:06+00:00
3             date:timestamp                     2024-03-08T10:48:16+00:00
4                   png:bKGD chunk was found (see Background color, above)
5    png:IHDR.bit-depth-orig                                             8
6         png:IHDR.bit_depth                                             8
7   png:IHDR.color-type-orig                                             2
8        png:IHDR.color_type                                 2 (Truecolor)
9  png:IHDR.interlace_method                            0 (Not interlaced)
10     png:IHDR.width,height                                    3000, 1800
11                  png:pHYs             x_res=23622, y_res=23622, units=1

However, and that's the important part here, the png-file has 600dpi while the jpeg-file only has 96dpi on my machine. The reason for that seems to be that, according to the ggsave documentation, the dpi argument only applies to 'raster output types', obviously not supporting jpeg-files yet.

Regarding the output for a journal, usually the request is limited to a minimum resolution (usually around 300 dpi) and certain supported image formats. Thus, the easiest workaround would be to see if they also accept png-files and go from there, using 600dpi if you want to:

# Load packages
library(dplyr)
library(ggplot2)

# Set working directory
setwd ("yourwd")

# Image processing and saving using pipe
(
iris %>%
  group_by(Species) %>%
  summarise(Mean = mean(Sepal.Length)) %>%
  ggplot(aes(x = Species,y = Mean)) +
  geom_bar(stat = "identity", color = "black",fill = "lightgrey")
) %>%
ggsave("Example.png", ., height = 3, width = 5, units = "in", dpi=600, device = "png") 

Make sure not to oversee , ., in the last line.