Shivam7898
Shivam7898

Reputation: 132

How to match the PNG image size between manually executing a chunk and knitting a file using knitr and ggsave

I have knitted a PDF using 'knitr' package. It also saves a PNG file in the directory using 'ggsave'.

What should I do so that PNG files generated in both cases should have same size and dimensions? If possible, I would prefer that the output to be 13.3 kb in both situations.

I have added code for minimum reproducible example. Please save it as a ".Rmd" File for generating PNG and PDF.

Thank You

title: "Image Size Mismatch"
output: pdf_document

EDIT: Smaller Code Example

# Packages
library("knitr")
library("ggplot2")

# Data
hh <- data.frame(groups = factor(c("No", "Yes", NA)), n = c(3843, 856, 19))

# Create Chart
pp <- ggplot(data = hh, aes(x = groups, y = n)) + geom_bar(stat = 'identity')

# Save Image
ggsave("Bar.png", plot = pp, device = "png", dpi = 144)

Older Example: It had some unnecessary code.

# Packages
library("knitr")
library("dplyr")
library("tibble")
library("ggplot2")

# Data
hh <- tibble(groups = factor(c("No", "Yes", NA)), n = c(3843, 856, 19))
loc_png <- "Pie.png"

# Create Pie Chart
pp <- ggplot(data = hh, aes(x = '', y = n, fill = groups)) +
    geom_bar(stat = 'identity', width = 1, color = "white") +
    coord_polar(theta = "y", start = 0) +
    # Add Text Labels
    geom_text(aes(label = paste0(groups, "\n", n)),
              position = position_stack(vjust = 0.5)) +
    # Theme and Labs
    theme(panel.background = element_rect(fill = "white", colour = "white"),
          legend.position = 'none', axis.text = element_blank(), 
          axis.ticks = element_blank(), axis.title = element_blank(), 
          panel.grid = element_blank()) + 
    labs(title = "Pie")

# Save Image
ggsave(loc_png, plot = pp, device = "png", dpi = 144)
# Problem: 
# Manual Chunk Execution: Output File: Size 32.5 kb, Dimension 1008 x 1008
# Knit "Ctrl +Shift +K" : Output File: Size 22.4 kb, Dimension 936 x 647

Upvotes: 0

Views: 185

Answers (1)

Bernhard
Bernhard

Reputation: 4417

You can take control over how large and with which aspect ratio a plot is drawn both in RMarkdown as well as with ggsave. For your purpose, you'll want to set both to the same values.

Adjusting plot size and thereby aspect ratio are described in https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html , the proper use of ggsave in help("ggsave").

The following minimal example is valid Rmd and uses both options from within the same file:

---
title: "Untitled"
output: pdf_document
---

```{r, fig.dim = c(5, 3)}
library(ggplot2)

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width))

ggsave("test.png", width = 5, height = 3, units = "in")
```

Honestly, I do not know, how many dots per inch (dpi) are used in the knitted document, my example appears to be written as a vector file, but if needed, that could also easily be adapted in the call of ggsave. Be the call of ggsave within the Rmd or outside of it.

Upvotes: 1

Related Questions