0

I was able to import the font Lato in my shiny web app from google and to using it, but I want to change the font in my ggplot2 graphics into Lato too, but with the family argument it doesnt work, because ggplot want to access only windows fonts.

Lato header

Here is my code:

library(shiny) library(shinyjs) library(dplyr) library(ggplot2) #library(showtext) ui <- shinyUI(fluidPage( tags$head(tags$style( HTML(' @import url("https://fonts.googleapis.com/css2?family=Lato:wght@300&display=swap"); #sidebar { background-color: white; } * { font-family: "Lato"; }') )), titlePanel("Hello Shiny!"), sidebarLayout( sidebarPanel(id="sidebar", sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30) ), mainPanel( plotOutput("distPlot") ) ) )) server <- shinyServer(function(input, output) { output$distPlot <- renderPlot({ x <- faithful[, 2] # Old Faithful Geyser data ggplot(data.frame(x), aes(x = x)) + geom_histogram( bins = 30) + labs(x = NULL, y = NULL, fill = NULL, title = "Title should be in Font Lato") + theme(plot.title = element_text(size=10, family = 'Lato')) }) }) shinyApp(ui=ui,server=server) 

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Zeichensatzfamilie in der Windows Zeichensatzdatenbank nicht gefunden

Any ideas why ggplot cannot find Lato?

Thanks

1 Answer 1

2

Using showtext this could be achieved by adding

font_add_google("Lato", "lato") showtext_auto() 

to your code:

library(shiny) library(shinyjs) library(dplyr) library(ggplot2) library(showtext) #> Loading required package: sysfonts #> Loading required package: showtextdb font_add_google("Lato", "lato") showtext_auto() ui <- shinyUI(fluidPage( tags$head(tags$style( HTML(' @import url("https://fonts.googleapis.com/css2?family=Lato:wght@300&display=swap"); #sidebar { background-color: white; } * { font-family: "Lato"; }') )), titlePanel("Hello Shiny!"), sidebarLayout( sidebarPanel( id = "sidebar", sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30 ) ), mainPanel( plotOutput("distPlot") ) ) )) server <- shinyServer(function(input, output) { output$distPlot <- renderPlot({ x <- faithful[, 2] # Old Faithful Geyser data ggplot(data.frame(x), aes(x = x)) + geom_histogram(bins = 30) + labs( x = NULL, y = NULL, fill = NULL, title = "Title should be in Font Lato" ) + theme(plot.title = element_text(size = 10, family = "lato")) }) }) shinyApp(ui = ui, server = server) #> #> Listening on http://127.0.0.1:8198 

EDIT Test with a different font.

font_add_google("Gochi Hand", "gochi") showtext_auto() 

Sign up to request clarification or add additional context in comments.

2 Comments

The error msg. is gone, but the title isnt unfortunatly in lato
Hm. Weird. In the reprex and when I run the app on my machine it's "Lato". I just added another image with a font where it's visible on first glance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.