I am using Shiny code in R to produce multiple(30) Boxplots. I have about 30 categories (x-axis) that I am trying to display side by side and want to adjust the x-axis on the bottom so that I can see each of the names at a 90 degree angle. I tried putting conditions in the 'ggplot' & the 'plotly' line to adjust the x-axis / y-axis but it does nothing on the Shiny app. I also want to adjust the y-axis tick marks so that they tick at every $1000 value. I can reproduce the graph using ggplot2 and adjust the x/y-axis, but want to make my graph dynamic with Shiny so I can apply different filters. I am trying to plot 'Name' (X-axis, categorical) vs 'Dollar' (Y-axis, numerical) and have about 30 different 'Names' I would like to view. My data has 10K+ rows.
Sample Data:
ID Tradelane Name Dollar
10R46 Ocean Ray 2000
10R41 Air Jay 1000
10R45 Truck Alfred 500
10R49 Ocean Mark 5
Would appreciate any help, below is my code:
library(shiny)
library(plotly)
data(supply)
nms <- names(supply)
ui <- fluidPage(
headerPanel("Supply Metrics"),
sidebarPanel(
sliderInput('sampleSize', 'Sample Size', min = 1, max = nrow(supply),
value = 1000, step = 500, round = 0),
selectInput('x', 'X', choices = nms, selected = "name"),
selectInput('y', 'Y', choices = nms, selected = "dollar"),
selectInput('color', 'Color', choices = nms, selected = "tradelane"),
selectInput('facet_row', 'Facet Row', c(None = '.', nms), selected = "tradelane"),
selectInput('facet_col', 'Facet Column', c(None = '.', nms)),
sliderInput('plotHeight', 'Height of plot (in pixels)',
min = 100, max = 2000, value = 1000)
),
mainPanel(
plotlyOutput('trendPlot', height = "900px")
)
)
server <- function(input, output) {
#add reactive data information. Dataset = built in diamonds data
dataset <- reactive({
supply[sample(nrow(supply), input$sampleSize),]
})
output$trendPlot <- renderPlotly({
# build graph with ggplot syntax
p <- ggplot(dataset(), aes_string(x = reorder(input$x, input$y, FUN = median) y = input$y, color = input$color)) +
geom_boxplot() + theme(axis.text.x=element_text(size=2,angle=90))
# if at least one facet column/row is specified, add it
facets <- paste(input$facet_row, '~', input$facet_col)
if (facets != '. ~ .') p <- p + facet_grid(facets)
a <- list(size=3, color = "black", tickangle = 45)
ggplotly(p) %>%
layout(height = input$plotHeight, xaxis = a, yaxis = a))
})
}
shinyApp(ui, server)