firmo23
firmo23

Reputation: 8404

Increase text size in a plotly column chart displayed in limited space

How to increase the size of text in a plotly column chart which has to be displayed many bars in a limited space? As you can see I tried to increase the size but it does not work. I also tried to change the text angle but same result.

library(shiny)
library(plotly)

df<-data.frame(Country = c("Austria", "Belgium", "Bulgaria", 
                 "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia", "Finland", 
                 "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", 
                 "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", 
                 "Malta", "Netherlands", "Norway", "Poland", "Portugal", "Romania", 
                 "Slovakia", "Slovenia", "Spain", "Sweden"), Value = c(21.1, 
                                                                                         20.9, 8.2, 14.7, 18.7, 15.8, 18.9, 24.1, 25.8, 20.5, 19.1, 16.2, 
                                                                                         35.9, 21.6, 20.4, 18.1, 7.8, 14, 20.6, 19.7, 34.4, 21.6, 19.9, 
                                                                                         19, 18.5, 14.6, 19.6, 19.3, 21.7, 18.3))



ui <- fluidPage(
  fluidRow(
    column(4,plotlyOutput("plot")),
    column(8,  
)
  )
)

server <- function(input, output) {
  output$plot<-renderPlotly({
    
    fig1 <- plot_ly(df, 
                    y = df$Value, 
                    x = df$Country,
                    type = 'bar', 
                    hovertemplate = paste('%{y}', 
                                          '<br>Uptake first dose (%): %{x}<br><extra></extra>'), 
                    text = paste(df$Value, '%'), 
                    textposition = 'outside',
                    #change color
                    textfont = list(color = "red",size=20),
                    marker = list(color = '#63bb47')
    ) 
    fig1 <- fig1 %>% layout(
      font = list(color = '#a2a2a2'),font = list(color = '#a2a2a2'),
      xaxis = list(fixedrange = TRUE,title="",
                   showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
      yaxis = list(fixedrange = TRUE,title="",zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE)) 
    
    
    fig1
  })
 
}

shinyApp(ui, server)

Upvotes: 0

Views: 545

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

You can use uniformtext in layout to control the text size.

library(shiny)
library(plotly)


ui <- fluidPage(
  fluidRow(
    column(4,plotlyOutput("plot")),
    column(8,  
    )
  )
)

server <- function(input, output) {
  output$plot<-renderPlotly({
    
    fig1 <- plot_ly(df, 
                    y = df$Value, 
                    x = df$Country,
                    type = 'bar', 
                    hovertemplate = paste('%{y}', 
                                          '<br>Uptake first dose (%): %{x}<br><extra></extra>'), 
                    text = paste(df$Value, '%'), 
                    textposition = 'outside',
                    #change color
                    textfont = list(color = "red",size=20),
                    marker = list(color = '#63bb47')
    ) 
    fig1 <- fig1 %>% layout(
      font = list(color = '#a2a2a2'),font = list(color = '#a2a2a2'),
      xaxis = list(fixedrange = TRUE,title="",
                   showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
      yaxis = list(fixedrange = TRUE,title="",zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE), 
      uniformtext=list(minsize=6, mode='show')) 
    fig1
  })
  
}

shinyApp(ui, server)

Upvotes: 1

Related Questions