I have a ggplot in Shiny, which uses geom_point to plot some data. I have it setup so that when a checkbox is checked, an aesthetic is added which colours the data into two separate variables. This also creates a legend. My problem is that when this legend appears, it 'takes' space from the plot and the plot becomes slightly smaller. Is there a way I can fix the size of the plot so that the legend appears without altering the plot size?
ui <- fluidPage(
titlePanel("Transfers Analysis App"),
sidebarLayout(
sidebarPanel(
checkboxInput("Outage", "Show Outages", FALSE)
),
mainPanel(
plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
verbatimTextOutput("hover_info")
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
Outage <- input$Outage
g <- ggplot(data, aes(Date, NUMBER_OF_TRANSFERS)) + geom_point()
if (Outage == TRUE)
g <- g + geom_point(aes(color = Outage)) + scale_colour_manual(breaks = c("Outage", "No Outage", "Day After an Outage", "Both"), name= "Legend", values=c( "black", "red", "blue")) + theme(legend.position="bottom")
plot(g)
})
}
shinyApp(ui, server)
note: my actual code has a lot more features in than this which I have cut out for simplicity.