Whitespace in ggplot donut chart

Hello folks,
I am facing an issue while plotting a donut chart using ggplot. I am able to plot the donut, but it comes with whitespace on either side. I assume this is because I am creating a bar chart and then folding it across Y-axis to obtain a donut. Is there a way to remove the whitespace from the generated plot.

Note: I do not want to fill the whitespace with the background color. I want that space to be removed.

library(ggplot2)
library(tidyverse)

df <- data.frame(
  "category"=c("A", "B", "C"),
  "color"=c("#98D278","#ea4f62","#f5a623"),
  "volume"=c(4321, 1200, 500)
)

ggplot(df, aes(fill=category, x = 2, y = volume))+
  geom_bar(stat = "identity", alpha=0.8, fill = df$color) +
  xlim(.2,2.5) +
  coord_polar(theta="y") +
  theme_void() +
  theme(panel.background = element_rect(fill = '#1b2036'))

Any help is appreciated.

Sorry but I can't reproduce your issue or I don't understand it, can you explain to what "whitespace" you are referring to?

library(tidyverse)

df <- data.frame(
    "category"=c("A", "B", "C"),
    "color"=c("#98D278","#ea4f62","#f5a623"),
    "volume"=c(4321, 1200, 500)
)

ggplot(df, aes(fill=category, x = 2, y = volume))+
    geom_bar(stat = "identity", alpha=0.8, fill = df$color) +
    xlim(.2,2.5) +
    coord_polar(theta="y") +
    theme_void() +
    theme(panel.background = element_rect(fill = '#1b2036'))

Created on 2020-04-20 by the reprex package (v0.3.0.9001)

This is how the plot opens in a black background. The white portion to the left and right of the actual donut chart is what I am referring to.

How are you exporting the image? Or in what context is being used?

I am using the resultant plot inside a fluidRow in shiny.

Can you provide a minimal reproducible example for that?

Hi Andres,
I was able to fix this by making a change in the server code. I used bg="transparent" and explicitly specified the color to be filled in the style of the plot.

library(ggplot2)
library(tidyverse)
library(shiny)

df <- data.frame(
  "category"=c("A", "B", "C"),
  "color"=c("#98D278","#EA4F62","#F5A623"),
  "volume"=c(4321, 1200, 500)
)
ui <- fluidPage(
  fluidRow(
    column(
      width=6,
      column(
        width=6,
        fluidRow(plotOutput("plot"), style = "height:400px; background-color: #1B2036;")
      )
    )
  )
)


server <- function(input, output) {
  output$plot <- renderPlot({
    ggplot(df, aes(fill=category, x = 2, y = volume))+
      geom_bar(stat = "identity", alpha=0.8, fill = df$color) +
      xlim(.2,2.5) +
      coord_polar("y")+
      theme_void()+
      theme(panel.background = element_rect(fill = '#1b2036',size = 0))
  },bg="transparent")  
}
shinyApp(ui = ui, server = server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.