Changing ggplot image size in flexdashboard - r-markdown

In flexdashboard,
if you use
vertical_layout: fill
then the image is stretched to entire screen
If you use
vertical_layout: scroll
then the image is shown at some other (predetermined?) size
Is that possible to change the image size in both cases?
Code example is below:
## ...
### Plot
```{r}
ggplot(diamonds) + geom_point(aes(carat, price))
```

Related

Rmarkdown plot and image side by side

I'm working on a report in R markdown.
I know there is a way to have different plots side by side, and there is also a way to have different images side by side.
But is it also possible to show a plot and an image side by side?
I have a ggplot bar graph that I would like to present next to an image of a map, it takes up too much space if I put the image below the graph.
Thanks,
Regards,
Freya
This could work to you. Here is the step by step, with an indication of the code between parentheses.
First, you need to load the image (y), by creating an object (photo).
Second, you create a ggplot with the image (photo_panel).
Finally, after creating your plot (p1), you use the library cowplot to plot a grid (plot_grid).
.Rmd file:
---
title: "Image + graph"
author: bttomio
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
```{r image_graph}
y = "http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg"
download.file(y,'y.jpg', mode = 'wb')
library("jpeg")
photo <- readJPEG("y.jpg",native=TRUE)
library(ggplot2)
library(cowplot)
photo_panel <- ggdraw() + draw_image(photo, scale = 0.8)
df <- data.frame(Years=rep(2016:2017, each=4),
Quarters=rep(paste0("Q", 1:4), 2),
Series1=seq(100, 800, 100))
library(ggplot2)
p1 <- ggplot(df) +
geom_point(aes(x=Quarters, y=Series1)) +
facet_wrap( ~ Years, strip.position="bottom", scales="free_x") +
theme(panel.spacing=unit(0, "lines"),
strip.background=element_blank(),
strip.placement="outside",
aspect.ratio=1) # set aspect ratio
plot_grid(p1, photo_panel, ncol = 2)
```
Output:

Change Default Beamer slide size

In R markdown when using Beamer slides if you try to plot you need to specify the plot sizes (unlike in the reports) so that the plots fit on a page. This can often result in the plots appearing to be squashed together, as apposed to just a smaller version of the plot.
Is there some method to change the default slide size to alleviate this problem?
I have tried
header-includes:
- \usepackage[papersize={25.6cm,19.2cm}]{geometry}
in the yaml header, and I get the error
! LaTeX Error: Missing \begin{document}.
Using R Markdown I shouldn't need to use this though.
A reproducible example is shown below
---
title: "Plots look bad"
author: "Beavis"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output: beamer_presentation
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(results = 'hide')
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(fig.height=3.5)
```
# Introduction
```{r}
pca <- prcomp(iris[,1:4])
biplot(pca)
```
Here, you if you run this the second slide looks like this As you can see the plot is rubbish. What is the best way to avoid this problem?
The biplot function uses par(pty = "s") to force a square plot, so it's not going to fill a rectangular slide. You can make it look better by asking the fig.height to be bigger, but then it will overflow the bottom of the slide. To prevent this, you can set both fig.height to a large number, and out.height (which will be a LaTeX measurement) to something that will fit on a slide. For example, using this chunk
```{r fig.height=10, out.height="0.8\\textheight"}
pca <- prcomp(iris[,1:4])
biplot(pca)
```
I see this output:
I'd recommend a smaller fig.height, but your preference may be different.
Both fig.height and out.height could be specified using knitr::opts_chunk$set as defaults for all slides if you want.
You could also specify out.width="\\textwidth" for a really ugly stretched plot that fills the slide, but I wouldn't recommend it.

How to add background colours to boxes on Rmarkdown flexdashboard

I would like to colour each box on my flexdashboard a different colour. For example, I would like the background inside box 1 to be blue, the background inside box 2 to be green and so on.
Would anyone be able to advise me on whether this is possible, and if so, how to do this please?
I have attached an example piece of code below.
I cannot use value boxes due to having more than one piece of information to input.
Many thanks,
title: "Example"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Column {data-width=450}
-----------------------------------------------------------------------
### Box 1
```{r}
x = 5
y =6
```
In this example `r x` is smaller than `r y`
### Box 2
```{r}
x = 5
y =6
z= 4
```
In this example `r x` is smaller than `r y` but bigger than `r z`
Column {data-width=450}
-----------------------------------------------------------------------
### Box 3
```{r}
```
### Box 4
```{r}
```
You can use CSS to style all elements of your dashboard. Your boxes are automatically assigned an ID corresponding to their title (usually just lowercase and spaces are replaced by hyphens):
<style>
#box-1 {
background-color: blue;
color: white;
}
#box-2 {
background-color: green;
}
Additionally it is possible to add CSS classes to your boxes like
## Box 1 { .first-box }
Then you can change the styles for this class using
<style>
.first-box {
...
}
</style>

How to insert an image from file into a PlotWidget (plt1 = pg.PlotWidget(w);)?

How to insert an image from file into a PlotWidget (plt1 = pg.PlotWidget(w);)?
The image is for a layout on which some calculated and plotted points should appear.
I tried to insert the image in a Qlabel behind the PlotWidget and make the PlotWidget transparent, but didn't work as transparency takes the color of the window not real transparency.
Thanks
Use QGraphicsPixmapItem:
plt1 = pg.PlotWidget(w)
img = pg.QtGui.QGraphicsPixmapItem(pg.QtGui.QPixmap(fileName))
plt1.addItem(img)
# depending on your preference, you probably want to invert the image:
img.scale(1, -1)
# OR invert the entire view:
plt.invertY(True)

Adjust the output width of R Markdown HTML output

I am producing an HTML output but I am having issues with the output width of R code output.
I'm able to adjust the figure width with no difficulty but when I try to write a data table or the factor loadings, R is outputting at a fixed width which is only about a third of my screen width. This results in the columns of the table being split up rather than all of the columns displayed in a single table.
Here is a reproducible example:
---
output: html_document
---
# Title
```{r echo = FALSE, fig.width=16, fig.height=6}
x = matrix(rnorm(100),ncol=10)
x
plot(x)
```
Add this at the start of your document:
```{r set-options, echo=FALSE, cache=FALSE}
options(width = SOME-REALLY-BIG-VALUE)
```
Obviously, replace SOME-REALLY-BIG-VALUE with a number. But do you really want to do all that horizontal scrolling?
Your output is probably being wrapped somewhere around 80 characters or so.
Also, you can temporarily change the local R options for a code chunk:
```{r my-chunk, R.options = list(width = SOME-BIG-VALUE)}
```