How can I reset my plot panel after using force_panelsizes of package gghx4 ? Since I have used this function on a ggplot (and decided not to use it) my plotting panel seems to be stuck with the setup caused by this function.
My plots look for example like this before and now (I had to blur some data due to privacy reasons):
BEFORE
AFTER
So the first plot uses almost the whole page, the second is still limited by width and height.
I already reinstalled my R version (4.1.0), R Studio version (2022.02.3), ggplot2 and my latex-version of tinytex. The plots are part of a knitted report. I had to blur some parts due to privacy reasons.
A MRE Example of the code I'm using (with minimal change in data for privacy reasons and minimal changes in the used margins for the title, caption and x-Axis because of the small plot area - otherwise no bars would have been plotted):
---
output:
pdf_document:
number_sections: true
classoption: a4paper
geometry: left=2cm,right=1cm,top=1.5cm,bottom=1cm,includeheadfoot
fontsize: 11pt
lang: de
header-includes:
- \usepackage{caption}
- \captionsetup[figure]{font={footnotesize,rm}}
- \captionsetup[table]{font={footnotesize,rm}}
- \usepackage{lastpage}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhf{}
- \fancyhead[R]{\fontsize{9}{11} \selectfont \leftmark}
- \fancyhead[L]{\fontsize{9}{11} \selectfont Spezialauswertung}
- \fancyfoot[R]{\fontsize{9}{0} \selectfont Seite \thepage\ von \pageref{LastPage}}
---
```{r echo=FALSE, warning=FALSE}
library(ggplot2)
df <- data.frame(cbind(
Pop=rep(1:3,8),
Question_Cat=rep(1:8,each=3),
Values=c(84,89,100,92,100,10,82,91,100,26,18,26,76,90,77,52,36,50,83,100,100,72,81,73),
n=c(129,11,19,90,11,19,101,11,19,127,11,19,126,11,18,120,11,7,127,11,19,125,11,19),
sort=rep(1:8,each=3))
)
lab <- c("Question 1","Question 2","Question 3","Question 4","Question 5","Question 6","Question 7","Question 8")
names(lab) <- as.character(1:8)
ggplot(data=df,aes(x=1,y=Values)) +
geom_bar(aes(group=Pop,fill=factor(Pop)),stat="identity",position="dodge") +
facet_wrap(~ sort,nrow=8,labeller=labeller(.default=lab,.multi_line = FALSE)) +
scale_fill_manual(values=c("#5ba755","#ddebf7","#acb9ca"),labels=c("Group 1","Group 2","Group 3")) +
coord_flip() +
ylim(-2,100) +
geom_text(aes(label=paste(round(Values,2),"%"),group=Pop),position = position_dodge(0.9),hjust=-0.4,vjust=0.35,size=rel(2)) +
geom_text(aes(label=paste("n=",n),y=0,group=Pop),position = position_dodge(0.9),hjust=1.2,size=rel(2)) +
theme_classic() +
theme(legend.position = "top", legend.title = element_blank(),legend.text=element_text(size=rel(0.6)),
plot.title = element_text(hjust = 0.5,size=rel(1.1),face="bold",margin=margin(25,0,5,0)),
plot.caption.position="plot",plot.caption = element_text(size=rel(0.7),margin=margin(0,0,0,0)),
strip.text.x = element_text(size=rel(0.8),face="italic",hjust=0),
strip.background = element_blank(),
axis.text.y=element_blank(),
axis.line.y=element_blank(),
axis.ticks.y=element_blank(),axis.title.y=element_blank(),
axis.title.x=element_text(size=rel(0.7),margin=margin(0,0,0,0)),
axis.text.x=element_text(size=rel(0.85),margin=margin(0,0,0,0))) +
guides(fill = guide_legend(reverse = TRUE)) +
ylab("Prozent") +
xlab("") +
labs(title="Some Plot Title here",subtitle="",caption = "Some explanation here")
```
The PDF-Output looks like this (still wrong):
In all 3 outputs header and footer are not shown.
I also tried to close the plotting panels with dev.off() but there is only the NULL device left:
Error in dev.off() : cannot shut down device 1 (the null device)
Any ideas of what went wrong?
Any help is really appreciated :))))))))))))))))
Thank you!
How do I tell to knitr to use theme_classic if I export my Rmd notebook to PDF and use dark_theme_gray from ggdark package if I export my Rmd notebook to HTML?
Try this. (Sorry. I had no ggdark installed. So I just used theme_gray for HTML output). knitr provides helper functions to check for is_html_output or is_latex_output.
---
title: "test"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Plot
```{r}
library(ggplot2)
p <- ggplot(mtcars, aes(hp, mpg)) +
geom_point()
if (knitr::is_html_output()) {
p + theme_gray()
} else if (knitr::is_latex_output()) {
p + theme_classic()
}
```
I want to embed a plot produced with ggplot and gganimate into a slideshow using knitr. I can produce animation when data and .Rmd file in same folder.
Here is a reproducible example for animation.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
library(gganimate)
library(gapminder)
```
## Static plot create
```{r, }
ranim <- ggplot(gapminder, aes(x = gdpPercap,y=lifeExp,
size = pop,
colour = country)) +
geom_point(show.legend = FALSE, alpha = 0.7) +
scale_color_viridis_d() +
scale_size(range = c(2, 12)) +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy")
```
## Static plot
```{r, }
ranim
```
## Build animate
```{r, }
ranim2 <- ranim +
transition_time(year) +
labs(title = "Year: {frame_time}")
```
## View animate
```{r, }
animate(ranim2)
```
However, the problem occurs when I used local data saved into a subfolder. I open a project in a folder 'Project1'. I save data in a subfolder 'Data'. I set the options to root directory into the data folder.
knitr::opts_knit$set(root.dir = './Data')
My .Rmd file is saved in the folder Project1. The code is below and produces a blank slide when I compile. I can produce the animated file in the Viewer by running the code chunk manually from the .Rmd. But when the html compiles it's blank.
Is there a recommended setup for organizing local data in project subfolder and producing markdown slides from .Rmd saved in main Project1 folder?
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_knit$set(root.dir = './Data')
library(ggplot2)
library(gganimate)
library(gapminder)
```
## slide 1
```{r, }
datain <- read.csv("table1.csv")
panim <- ggplot(datain, aes(x, y, frame = year)) + geom_point()
```
## Static plot view
```{r, }
panim
```
## Static plot add animate
```{r, }
panim2 <- panim + transition_time(year) +
labs(title = "Year: {frame_time}")
```
## Activate animate
```{r, }
animate(panim2)
```
Not sure what exactly your problem is, or what slide output styles you use, but this code works fine for for me (I get one slide with a static image and one slide with an animation).
---
title: "presentation"
author: "Me"
date: "20 FEB 2019"
output: ioslides_presentation
---
## slide 1
```{r,warning=F}
library(ggplot2)
library(gganimate)
library(gapminder)
```
## slide 2
```{r}
ranim <- ggplot(gapminder, aes(x = gdpPercap,y=lifeExp,
size = pop,
colour = country)) +
geom_point(show.legend = FALSE, alpha = 0.7) +
scale_color_viridis_d() +
scale_size(range = c(2, 12)) +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy")
```
## slide 3
```{r}
ranim
```
## slide 4
```{r}
ranim2 <- ranim +
transition_time(year) +
labs(title = "Year: {frame_time}")
```
## slide 5
```{r}
animate(ranim2)
```
Here's a simple markdown file:
---
title: "blah"
output: html_document
---
```{r}
library(tidyverse)
ggplot(tibble(x=1:2)) +
aes(x=x, y=x) +
geom_col() +
labs(y = "← low high →")
```
Notice the arrows. They show up when running the code via the console to RStudio's plot tab. But for an HTML knit, they don't work:
Use the unicode instead of the actual character:
library(tidyverse)
ggplot(tibble(x=1:2)) +
aes(x=x, y=x) +
geom_col() +
labs(y = "\u2190 low high \u2192")
I am creating a report with multiple figures and tables. I'd like to refer to them in the accompanying text. I've tried the following:
---
title: "Test"
output:
pdf_document
---
Figure \ref{test} is a graph
```{r test, fig.cap="This is a graph"}
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30))
ggplot(df, aes(x = gp, y = y)) +
geom_point()
```
This is text to follow the diagram
\pagebreak
This is another page but can still link to Figure \ref{test}
But the result is:
Figure ?? is a graph
...
This is another page but can still link to Figure ??
Is there a default way to do this in R markdown without having to write functions myself
I think I found an answer here- https://github.com/yihui/knitr/issues/323
Using this code seemed to provide the behavior I think you're looking for, if I'm understanding correctly.
---
title: "Test"
output:
pdf_document
---
Figure \ref{fig:plot} is a graph
```{r plot-ref, fig.cap = "This is a graph\\label{fig:plot}"}
library('ggplot2')
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30))
ggplot(df, aes(x = gp, y = y)) +
geom_point()
```
This is text to follow the diagram
\pagebreak
This is another page but can still link to Figure \ref{fig:plot}