I'm trying to apply a nice font to a ggplot rendered in a Shiny app.
Setting the desired font in RStudio (on the same server) using family="[fontname]" works correctly. Here a "serif" font family has been requested:
Image of correct ggplot font family rendering in rstudio
However, when ggplot is then embedded in the Shiny renderPlot({}) function, the font family doesn't change from the default. Here the same "serif" font family has been requested:
Image of incorrect ggplot font family rendering in Shiny app
Changes to the font size and font face (bold, italic) work as expected. I've checked the installed font names using fonts() and pdfFonts() within RStudio and within the shiny app, and then tried those listed as well as "serif", "sans", and "mono", all to no avail. I've also tried loadfonts().
A minimal example:
server.R
require(ggplot2)
require(ggthemes)
require(extrafont)
shinyServer(function(input, output) {
df <- data.frame(a=rnorm(100), b=rnorm(100))
output$the_plot <- renderPlot({
p <- ggplot(df, aes(x=a, y=b), environment=environment()) +
xlab("Alpha") +
ylab("Beta") +
geom_point() +
theme(text=element_text(family="serif", size=16))
print(p)
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
h6("Font test")
),
mainPanel(
plotOutput("the_plot")
)
)
))
Edit: There is a similar unanswered question but seeking pdf rather than png output. Have now also tried R base graphics instead of ggplot, with the same result.
As a workaround, I recreated much of the renderPlot() functionality using renderImage(), as described in this Shiny tutorial article. Happily this renders antialiased fonts galore. Hope this is of use to someone else.
ui.R amended to
server.R