6

I have an interactive display consisting of a bar chart that shows a selected statistic for different categories. However, ggplot2 readjusts the y-axis width depending on the labels, and hence makes the bars annoyingly move on the x-direction. See exemple:

library(shiny)
library(dplyr)
library(ggplot2)

shinyApp(
  ui = bootstrapPage(
    selectInput('statistic', label='Chose a statistic', choices=c('carat', 'depth', 'table', 'price')),
    plotOutput('plot')
  ),
  server = function(input, output) {
    output$plot <- renderPlot(
      diamonds %>%
        ggplot(aes(x=color, y=get(input$statistic))) +
        geom_bar(stat = 'sum') +
        theme(text = element_text(size=20), legend.position="none")
    )
  }
)

How can I fix the width of the y-axis label? (or equivalently the width of the plotting panel?)

I did find related questions, but all were solved in a static context, for instance with facets or with gtable.

4
  • This generates errors for me. And btw, you ought to use aes_string instead of aes(..., get(...)). Commented Jan 8, 2016 at 13:07
  • can you be more specific about the error? I've just run it and it works here
    – Arthur
    Commented Jan 8, 2016 at 13:21
  • What are your package versions? I have ggplot2: v1.0.1, shiny: 0.12.2 Commented Jan 8, 2016 at 13:25
  • there's a newer ggplot2 version (2.0.0)
    – Arthur
    Commented Jan 8, 2016 at 13:33

1 Answer 1

5

One way would be to provide a label function that normalizes the label widths by padding them with spaces. function(label) sprintf('%15.2f', label) would pad with 15 spaces on the left and print the numeric value with 2 decimal places.

library(shiny)
library(dplyr)
library(ggplot2)

shinyApp(
  ui = bootstrapPage(
    selectInput('statistic', label='Chose a statistic', choices=c('carat', 'depth', 'table', 'price')),
    plotOutput('plot')
  ),
  server = function(input, output) {
    output$plot <- renderPlot(
      diamonds %>%
        ggplot(aes(x=color, y=get(input$statistic))) +
        scale_y_continuous(labels=function(label) sprintf('%15.2f', label)) +
        geom_bar(stat = 'sum') +
        theme(text = element_text(size=20), legend.position="none")
    )
  }
)
4
  • Another option would be to rotate the labels so that they are parallel to the Y axis. Commented Jan 8, 2016 at 15:19
  • 6
    difficult to reproduce without my data but it comes from the font 'cause using axis.text.y = element_text(family='mono') in theme fixes it
    – Arthur
    Commented Jan 8, 2016 at 15:20
  • Yes, a single width font will do it. Commented Jan 8, 2016 at 15:20
  • Getting this to stop 'jiggling' wo a fixed-width font was difficult. I settled on a workaround using ggdraw() + draw_plot(..., width=1) + draw_plot(..., width=.998) + ... Commented Jun 3, 2016 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.