Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce/remove plot margins in Shiny apps including wordclouds

If I replicate this example of R Shiny app on my computer - full code and data here - the image generated by Shiny has significant white margins that end up cutting some of the words.

Here is an example of the image generated: example wordcloud

Notice that the white space is included in the actual png file generated by shiny, not from white space around the image. The example functions correctly when I see it online from the rstudio servers.

Possible hint: if I increase the resolution (e.g. by setting res = 150 in renderPlot), this white margin increases further. See output image: larger white margin

How can I remove this white space completely?

Edit: by increasing canvas size (width and height in plotOutput), and keeping the same resolution in renderPlot (or by reducing the resolution there), you are less likely to encounter the problem. But the white margin is still there. I want to remove completely the white margin generated by the plot, as in my real life case I have more complex wordclouds, comparison.cloud, etc.

like image 516
giocomai Avatar asked Jan 09 '18 15:01

giocomai


2 Answers

You can escape this problem by defining the plot size in pixels.

In the demo code this way:

# Show Word Cloud
mainPanel(
  plotOutput("plot",width = "500px", height="500px")
)
like image 149
Borislav Aymaliev Avatar answered Oct 23 '22 03:10

Borislav Aymaliev


I eventually managed to deal with it by adding par(mar = rep(0, 4)) before each call to wordcloud in the server.R file. I don't know if this leads to potential other issues, but for the moment it seems to answer my own question (it removes the white margins from wordclouds in shiny).

like image 22
giocomai Avatar answered Oct 23 '22 04:10

giocomai