3

The text in ggplots on my deployed Shiny app is waffling unpredictably between the desired size and shrunken font sizes.

When I host the app locally, all the text looks as I want it to, always. But when I rsync the exact same files over to my server and visit the requisite URL, half of the time the text on ggplots is tiny.

Here is my app being served in Chrome locally through RStudio, with the font as desired:

Local plot with correct font size

Here is the deployed app being served in Chrome, with the font small (note other attributes like weight and family are all consistent):

Deployed plot with small font size

The only place I define font sizes is in the ggplot2 theme, which seems innocuous:

  theme(plot.title= element_text(family="GT America", face='bold'),
        text = element_text(family="GT America", size = 16),
        plot.margin = unit(c(1,1,4,1), "lines"),
        legend.position = c(.5, -.22), legend.direction="horizontal",
        legend.background = element_rect(fill=alpha('lightgray', 0.4), color=NA),
        legend.key.width = unit(1, "cm"),
        legend.text = element_text(size=16))

What's worse, the problem seems to be intermittent - sometimes, the server shows the right font sizes; other times, the font sizes are shrunk. Because of this intermittency, I can't just change the font sizes on the server. I did this before, and when the problem waffled away, I had massive fonts in my ggplot outputs on my production server! Not ideal.

It's not completely browser-specific, because the same problem arises in Safari.

I've seen solutions to similar problems about font family that recommend using renderImage() rather than renderPlot(). But I'd rather not do that, as I'd eventually like to add an interactive hover tool to the plot, and I imagine that's not possible with an image.

I've also seen answers about screen size, but I don't understand how that could be causing this, as I'm working consistently on the same screen and same browser.

What could I be doing in my server settings, Shiny config, CSS, browser, etc. to cause this weird, on-again-off-again problem? I can share more details if useful.

lauren.marietta
  • 2,125
  • 1
  • 11
  • 19
  • Hey Lauren...I am going to throw a wrench in your gears and suggest that you use `rbokeh` instead of `ggplot`, because it will natively allow the hover, and because it is a JS package with an rShiny wrapper it plots nicely with none of those odd plotting conundra. It is a more practical interactive method for plotting in Shiny – sconfluentus Apr 10 '20 at 22:50
  • check this out and see if switching to this package solves the problem: https://rdrr.io/cran/rbokeh/man/rbokehOutput.html – sconfluentus Apr 10 '20 at 22:52
  • ok! I've had good experiences with Bokeh in Python. I'll try that out whenever I have a moment to play around with rewriting everything. Thanks for the tip. – lauren.marietta Apr 11 '20 at 15:29
  • Two years later and I still have the same problem! @lauren.marietta have you ever figured out a solution without having to switch to rbokeh? The first rendered version of the plot shrinks the text size, but if I zoom in and zoom out again, the text size is as expected. – trangdata Jun 09 '22 at 15:59
  • sadly, no. I ended up switching to plotly (not bokeh) and haven't looked back. – lauren.marietta Jun 10 '22 at 19:43

0 Answers0