How to remove white margins from pie chart in ggplot

Viewed 376

I'm trying to change the background color of my pie chart, in order for the plot to fit in better with my distill website. Unfortunately, the color change leaves a white "border" on the left- and right-hand side.
Does anyone know how I can remove those borders or change their color? All help is appreciated!

This is what my (unfinished) plot looks like right now.

My code looks like this:

bechdel2 %>%
  filter(country == "New Zealand") %>% 
  count(criteria) -> data_kreis

ggplot(data_kreis, aes(x="", y=desc(n), fill=criteria)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +
      theme_minimal()+
      geom_text(aes(label = paste0(n)), position = position_stack(vjust = 0.5)) +
      scale_y_continuous(breaks = 0:14) +
      labs(x = NULL, y = NULL, fill = NULL,
           caption = "Ergebnisse des Bechdel-Tests: Anzahl der Filme") +
  theme(
        axis.title = element_blank(), 
        axis.ticks = element_blank(), 
        axis.text = element_blank(),
        panel.background = element_rect(fill = "#FFF5DE",  color = "transparent"),
        panel.border=element_blank(),
        plot.background = element_rect(fill = "#FFF5DE",  color = "transparent"), 
        panel.grid = element_blank(),
        legend.background = element_rect(fill = "white", color = "black"),
        plot.caption = element_text(size = 12, hjust = 0.5),
        strip.background = element_rect(fill = "#FFF5DE",  color = "transparent"))

This is my output from dput(head(data_kreis)):

structure(list(country = c("New Zealand", "New Zealand", "New Zealand", 
"New Zealand"), criteria = structure(1:4, .Label = c("Es gibt weniger als 2 Frauen", 
"Frauen reden nicht miteinander", "Frauen reden nur über Männer", 
"Film besteht alle Kriterien", "Bewertung ist unsicher"), class = c("ordered", 
"factor")), n = c(4L, 6L, 2L, 2L)), row.names = c(NA, -4L), groups = structure(list(
    country = "New Zealand", .rows = structure(list(1:4), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
1 Answers

I've found that when I save the plot as a png with a transparent background and implement it into my distill website as a picture the white "borders" don't show. So my solution is to change the code as follows:

p <- ggplot(data_kreis, aes(x="", y=desc(n), fill=criteria)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +
      theme_minimal()+
  scale_fill_manual(values = c("Es gibt weniger als 2 Frauen" = "#3C5186",
                               "Frauen reden nicht miteinander" = "#5371bd",
                               "Frauen reden nur über Männer" = "#6488e8",
                               "Film besteht alle Kriterien" = "#9B72AA",
                               "Bewertung ist unsicher" = "#C6B4CE"
                               )) +
      geom_text(aes(label = paste0(n), fontface = "bold"), position = position_stack(vjust = 0.5), color = "white") +
      scale_y_continuous(breaks = 0:14) +
      labs(x = NULL, y = NULL, fill = NULL,
           caption = "Ergebnisse des Bechdel-Tests: Anzahl der Filme") +
  theme(
        axis.title = element_blank(), 
        axis.ticks = element_blank(), 
        axis.text = element_blank(),
        panel.background = element_rect(fill = "#FFF5DE",  color = "transparent"),
        panel.border=element_blank(),
        plot.background = element_rect(fill = "#FFF5DE",  color = "transparent"), 
        panel.grid = element_blank(),
        legend.background = element_rect(fill = "white", color = "black"),
        plot.caption = element_text(size = 12, hjust = 0.5),
        strip.background = element_rect(fill = "#FFF5DE",  color = "transparent"),
        text=element_text(family = "sans"))

ggsave(p, filename = "images/Kreis_New_Zealand.png", bg = "transparent")
Related