How to make plotly charts adjust to page width using rmarkdown? - r

I have an R blog post with some leaflet and plotly figures. I can set width = "100%" in the leaflet() function, and the leaflet map will change shape as I resize my browser window (or view on mobile). I've tried the same width = "100%" in plot_ly(), but the plot doesn't resize for smaller windows and requires horizontal scrolling.
Any ideas? I'm using blogdown/hugo if that is helpful.

Use the chunk option:
```{r, out.width='100%'}

Related

Flexdashboard storyboard: including text above graphics in the main frame area

I want to include some text above the graphics in the main area of a frame in Flexdashboard storyboard. Here's the minimal example.
---
title: "Title"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
horizontal_layout: scroll
runtime: shiny
---
Storyboard name {.storyboard}
=====================================================================
### Storyboard frame 1
```{r}
renderPlot({
plot(rnorm(1e3))
})
```
>Footnote
***
Data commentary text.
When I add the text just above and outside the R chunk, the graph gets truncated at the bottom.
The graph needs to be inside renderPlot() to include interactive elements. It appears the problem is because the height parameter of renderPlot() is "auto", which automatically sets the height of the graphic to the height of the whole frame, and since the text is included, the height that it sets means the graph doesn't fit the area less the text and gets truncated. I found by experimenting the height can be set to a fixed size in pixels. I can set the height in pixels by trial-and-error that would look OK on my screen, but unfortunately it doesn't fully solve the problem, because users who open the app in a different screen resolution would get a messed up layout.
Personally speaking, fixing the height is not a big issue, fixing the width is the bigger problem for users with different screens. Both overflow height and width can be converted to a scrollable page, but it is always comfortable to scroll the document vertically but not horizontally, and this is what most websites are doing. Anyhow, if you don't want to fix the height of the plot with px, the easiest way is to add a scroll bar to the left column so users can scroll and see the hidden content:
Add this chunk and you are all set.
```{css}
.chart-wrapper .chart-stage {
overflow-y: auto;
}
```

Plotly width larger than content

I'm using Plotly (ggplotly) in my rmarkdown file. The width of the plot is larger than the content, see attached screenshot.
As you can see the legend is outside the content.
Can I fix this using CSS, if so, how?
I just found out that I had set a default fig.width and fig.height in knitr::opts_chunk() which caused this.

Distortion when using ggplot to plot shapefile

I've been using ggplot to plot my map using shapefile.
The plot will change its size when I resize the window (I'm using Rshiny).
I've tried to fix the height of the plot like:
plotOutput(outputId = "plot", height = "700px", width = "100%")
However, it will lead to distortion of map, and a scroll bar occurs which I don't want it. Is there any way to keep my map plotting without distortion, and can resize when the window size change?
Thank you so much!

In R can I set dygraph to resize to window size?

In R can I set dygraph to resize to browser window size?
(for example as percentage of browser view?)
I can see in link below that dygraph can do it but from R I only seem to be able to set a fixed width in pixels.
http://dygraphs.com/tests/resize.html
Any suggestions? Thanks.
You could set the width parameter to auto:
dygraphs::dygraph(data = ..., width = 'auto')
I had a similar issue using using dygraph through renderUI in the body of a shiny app.
auto allowed to use full body width.

Set width and height of graphic made using ggplot, grid, and gridExtra

If I have a graphic composed of several plots, say three plots arranged vertically. This is a gtable object and can be drawn to the page with:
grid::grid.newpage()
grid::grid.draw(plot)
However I see that the plot in my RStudio is 'smushed up' as in the screenshot below:
As you can see in the bottom right corner it is squashed and the titles overlap with other elements of the graphic.
If I hit zoom and view the plot it is a lot bigger:
Now I know, that if I were to export my gtable plot using pdf() or png() and such devices, I can set a width and a height, and so just make it big enough such that the plot is not squashed.
However, instead of one of those graphic devices, I would like to use export.grid, from the gridSVG package to save it to an SVG file. But if I do
gridSVG::export.grid(plot)
Then the SVG file exported looks squashed as it does in the RStudio plot window.
So my question is, how can I manipulate the dimensions of the graphic so it is drawn to SVG without it looking squashed? I draw the plot initially with grid.newpage and grid.draw, I wonder perhaps I have to specify some size of the page or drawing using grid.
Thanks,
Ben.

Resources