Home > Blockchain >  Making pie chart's borders smoother with ggplot on R
Making pie chart's borders smoother with ggplot on R

Time:03-18

I performed a script to obtain a pie/donut chart in R with ggplot.

Here my script

    ggplot(dat1, aes(x = treatment, y = data, fill = group, color = treatment))  
  geom_col(size=.8)  
  scale_color_manual(values=c("black", "gold3"))  
  scale_fill_brewer(palette="Paired")  
  coord_polar("y")  
  geom_text(aes(label = data), position = position_stack(vjust = 0.5), size =4, fontface="bold")  
  theme_void()  
  ggtitle ("Title")  
  theme(plot.title = element_text(hjust = 0.5, size = 25))

It worked exactly as I wanted, except that the borders is quite crappy, as you can see in the figure, it seems free hand drawn.

chart borders

Is there any way to make the borders more precise and smooth, like a perfect circle?

Thank you for your always invaluable help and insights.

CodePudding user response:

I only add this as an answer to share the images. This isn't due to ggplot2 output. No matter how big or small I exported the image, I did not get the output to reflect what you have.

What circumstances led you to the image you have? Was it a screenshot in RStudio? Was it an exported image? Was it in R Markdown?

enter image description here

enter image description here

enter image description here

CodePudding user response:

As a potentially helpful way of getting an editable version, you could try the export package (only on GitHub at the moment) which gives you a smooth editable vector image in a powerpoint slide:

# devtools::install_github("tomwenseleers/export")
library(ggplot2)

df <- tibble::tribble(
  ~group, ~treatment, ~data,
     "A",        "q",    60,
     "B",        "q",    40,
     "C",        "q",     0,
     "D",        "q",     0,
     "E",        "q",     0,
     "F",        "q",     0,
     "G",        "q",     0,
     "H",        "q",     0,
     "I",        "q",     0,
     "K",        "q",     0,
     "L",        "q",     0,
     "J",        "q",     0,
     "A",        "w",    70,
     "B",        "w",    10,
     "C",        "w",     0,
     "D",        "w",     0,
     "E",        "w",     5,
     "F",        "w",     5,
     "G",        "w",     1,
     "H",        "w",     1,
     "I",        "w",     1,
     "K",        "w",     1,
     "L",        "w",     5,
     "J",        "w",     1
  )


g <- ggplot(df, aes(x = treatment, y = data, fill = group, color = treatment))  
  geom_col(size=.8)  
  scale_color_manual(values=c("black", "gold3"))  
  scale_fill_brewer(palette="Paired")  
  coord_polar("y")  
  geom_text(aes(label = data), position = position_stack(vjust = 0.5), size =4, fontface="bold")  
  theme_void()  
  ggtitle ("Title")  
  theme(plot.title = element_text(hjust = 0.5, size = 25))

export::graph2ppt(g, "output_graph.pptx")
#> Exported graph as output_graph.pptx

Created on 2022-03-17 by the reprex package (v2.0.1)

  • Related