Skip to content

Instantly share code, notes, and snippets.

@jthomasmock
Created November 16, 2022 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

RMarkdown version

---
output: html_document
---

```{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))
```

Using 'angle=90'

```{r, out.extra='angle=90'}
p
```

Using "style='transform: rotate(90deg);'"

```{r, out.extra="style='transform: rotate(90deg); margin-top: 50px;'"}
p
```

@Haly-en
Copy link

Haly-en commented Nov 17, 2022

Hi Tom...Thanks for such a quick response.

I am trying to render to a pdf format. I tried all three options, the first one with the grid works to rotate the graph but it doesn't respect the margins or the size of the graph. The last two definitely don't work. :(
I also tried saving the graph and including it as an image... I still can't do it but I keep trying.
`

another example i tried

\newpage
\begin{landscape}
Landscape:

summary(cars)

\end{landscape}
`

@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