Skip to content

Instantly share code, notes, and snippets.

@jthomasmock
Created November 16, 2022 21:57
Show Gist options
  • Save jthomasmock/293a5a51ea4f536b6548a8abb1a8f04b to your computer and use it in GitHub Desktop.
Save jthomasmock/293a5a51ea4f536b6548a8abb1a8f04b to your computer and use it in GitHub Desktop.
Rotating images
---
format: html
---
```{r}
library(ggplot2)
library(grid)
```
```{r}
#| warning: false
grid::grid.newpage()
p <- ggplot(mtcars, aes(x = disp,y = mpg)) + geom_point()
print(p, vp=grid::viewport(angle=-90))
```
```{r}
#| echo: fenced
#| out-extra: "style='transform: rotate(90deg); margin-top: 75px;'"
p
```
```{r}
#| echo: fenced
#| out-extra: 'angle=90'
p
```
```{r}
ggsave("norm-plot.png", p)
```
@jthomasmock
Copy link
Author

jthomasmock commented Nov 17, 2022

Howdy @Haly-en - as far as I know out.extra and out-extra are arguments passed to CSS/LaTeX layer - while they may work, it might be dependent on LaTeX packaging?

I am not at all a LaTeX expert, and there may be LaTeX native methods, you may want to reference: https://latex.org/forum/viewtopic.php?t=9323

Or could rotate the image after saving it on disk with magick:

ggsave("img.png", gg_obj)

library(magick)
plot_img <- image_read("img.png")

# rotate
image_rotate(plot_img, 90)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment