Either plotOutput
or renderPlot
seems to add a bunch of extra white space around a plot. I\'ve added background colours to the plot and the layout
I had a similar, but not identical, issue with a map. I knew what aspect ratio I wanted in the map (exactly 1:1 for me), much like you have with your pie chart, and I wanted it to occupy as much of the width of the responsive column as it could, changing the height accordingly. However, I didn't want it to be too big, so I added a line of logic to cap it to 400 pixels wide.
My approach was to draw a dummy ggplot
object that was invisible, then query the client session to learn about its size. Then I could pass that size as an explicit parameter to the actual desired plot.
# you need to include `session` as a third argument here
server <- function(input, output, session) {
# blank (plot for width) ----
output$blank <- renderPlot({
ggplot(data.frame(x = 1), aes(NA, NA)) + geom_blank() + theme_void()
})
blankwidth <- reactive({
# this is the magic that makes it work
bw <- session$clientData$output_blank_width
if (bw > 400) 400 else bw
})
blankheight <- reactive({
blankwidth() / 1.25
# whatever aspect ratio you need
})
output$plotofinterest <- renderPlot({
ggplot(iris[sample.int(150,50),], aes(1, fill = Species)) +
geom_bar() + coord_polar(theta = "y")
}, height = blankheight, width = blankwidth)
# this is the necessary bit
}
# ui.R
ui <- fluidPage(
fluidRow(
column(4,
style = "background-color:lightgreen",
plotOutput('blank', width = '100%', height = 0),
plotOutput('plotofinterest', inline = T)
),
column(8,
style = "background-color:lightblue",
HTML("filler")
)
)
)
shinyApp(ui, server)
If you make the window narrower, the plot will be less than the 400px cap, and it will take up the whole column. If you make it wider, the right-side green space gets larger. I haven't noticed a big loss of performance from drawing extra plots to get this info back from the client. Even when you drag around the window size to play with it, it updates quite quickly.
You do need to figure out what aspect ratio you want it drawn at. grid
will automatically add whitespace like Claus explained to the unrestricted pair of margins, so it's fair to just kind of eyeball it until it looks good to you.