Arthur
Arthur

Reputation: 2410

Customizing plot DPI in markdown presentation

I'm struggling to control the size and DPI of the images in my markdown presentation. I'm using the xaringan package.

The chunk options for fig.width and fig.height work intuitively until I change the dpi setting. For higher dpi settings, I can't seem to produce an image with appropriate size and appearance. Increasing dpi makes the image huge; I can compensate by decreasing fig.height and fig.width, but that ruins the scaling of the plot area and font sizes. Any help is appreciated!

I included some code, but I put it all in comments because otherwise StackOverflow will get confused about formatting the chunk options.

# ---
# title: Test plot appearance
# output:
#   xaringan::moon_reader:
#       css: [chocolate-fonts]
#       lib_dir: libs
#       nature:
#       highlightStyle: github
#       countIncrementalSlides: false
# ---
# 
# ```{r include = FALSE}
# library(ggplot2)
# ```
# 
# # Default DPI is low res
# ```{r echo = FALSE, fig.width = 6, fig.height = 4}
# ggplot(mtcars) + aes(cyl, mpg) + geom_point()
# ```
# 
# ---
# # 300 DPI makes image huge
# ```{r echo = FALSE, fig.width = 6, fig.height = 4, dpi = 300}
# ggplot(mtcars) + aes(cyl, mpg) + geom_point()
# ```
# 
# ---
# # I can compensate with small fig.width and height, but looks ugly
# ```{r echo = FALSE, fig.width = 2, fig.height = 2, dpi = 300}
# ggplot(mtcars) + aes(cyl, mpg) + geom_point()
# ```
# 
# ---
# # out.width and out.height cause image not to appear
# ```{r echo = FALSE, out.width = 6, out.height = 4, dpi = 300}
# ggplot(mtcars) + aes(cyl, mpg) + geom_point()
# ```

Upvotes: 2

Views: 1106

Answers (1)

Emi
Emi

Reputation: 3574

I think what you may want is the fig.retina chunk option.

E.g., when fig.retina = 2, the physical size of an image is doubled and its display size is halved.

In terms of out.width or out.height, the input needs to be character. If you want to use this option, you may like to use it like out.width = "80%".

See more plot options here.

Upvotes: 2

Related Questions