I have two plots that are located one above the other in a Shiny app.
The two plots have the same x axis, but different y axes.
The width of the x axes is different due to the length of the y axes labels (see image below).
The goal is to align the x axes of the two plots.
Minimal example:
library(shiny)
library(ggplot2)
df <- data.frame(
stringsAsFactors = F,
date = as.Date.factor(c("2010-01-01", "2011-01-01", "2012-01-01")),
var1 = c(1000000, 2000000, 1500000),
var2 = c(10, 15, 20)
)
shinyApp(
ui = fluidPage(
fluidRow(column(4, offset = 4, plotOutput(outputId = "plot1"))),
fluidRow(column(4, offset = 4, plotOutput(outputId = "plot2")))
),
server = function(input, output, session) {
output$plot1 <- renderPlot(
ggplot(data = df, mapping = aes(x = date, y = var1)) +
geom_line() +
scale_x_date(breaks = df$date)
)
output$plot2 <- renderPlot(
ggplot(data = df, mapping = aes(x = date, y = var2)) +
geom_bar(stat = "identity") +
scale_x_date(breaks = df$date)
)
}
)