Advanced Pie Chart Customization in R - ggplot Tutorial 12

Hey everyone, and welcome back. In today's video, we're going to be doing some advanced pie chart customization. In a previous post, I showed you how to make a basic pie chart. But in today's video, we're going to take it to the next level and customize the background, the individual color of the pie slices, and modify the legend. So if you're looking to make a simply fabulous pie chart, then this is the post for you.

Getting Started

For the purposes of this tutorial, I'm going to assume that you already have a basic pie chart created. If you're completely lost on how to create a pie chart, feel free to check out a previous post, or you can use the sample code that I have provided below. Once you run this code, you will get a very basic pie chart that looks something like this.

install.packages("tidyverse")
library (tidyverse)


my_data <- data.frame(
  CYL = c("4_cyl","6_cyl", "8_cyl"),
  Counts = c(14,7,14)
)


ggplot(data= my_data, aes(x = "", y = Counts, fill = CYL))+
  geom_bar(stat = "identity")+
  coord_polar(theta = "y")+
  theme_void()

image.png

Let's Customize!!

Once we have created our basic pie chart, we can start doing customization operations by using the theme() command. I know it can be intimidating to see a massive wall of code staring at you, but don't worry, we're going to break everything down nice and simple. But first, I want to provide the sample code and then show you the type of plot that it will create.

library (tidyverse)

my_data <- data.frame(
  CYL = c("4_cyl","6_cyl", "8_cyl"),
  Counts = c(14,7,14)
)

label = c("4 cyl","6 cyl", "8 cyl")

ggplot(data= my_data, aes(x = "", y = Counts, fill = CYL))+
  geom_bar(stat = "identity")+
  coord_polar(theta = "y", start = pi/2)+
  theme_void()+
  labs(title = "Prop of Cars by Cylinder")+
  geom_text(label = label, position = position_stack(vjust = .5),
            color = "black", size = 10)+
  theme(
    plot.background = element_rect(fill = "black"),
    plot.title = element_text(color = "white", hjust = .5),
    legend.text = element_text(color = "white"),
    legend.title = element_text(color = "white", hjust = .5),
    legend.background = element_rect(color = "white", fill = "black")
  )+
  scale_fill_manual(
    values = c(
      "4_cyl" = "white",
      "6_cyl" = "blue",
      "8_cyl" = "red"
    )
  )

The code above creates the following plot. Note the annotations that I have provided to indicate what commands change what elements of the plot.

annotated pie chart.jpg

Additional Notes

The start = pi/2 command in the code rotates the pie chart. You can also enter simple numeric values.

Notice that the plot has white bars in the margin. No matter how much we try, it can be difficult to get rid of them. To remove the white bars, we save the plot using the following code.

x <- ggplot(data= my_data, aes(x = "", y = Counts, fill = CYL))+
  geom_bar(stat = "identity")+
  coord_polar(theta = "y", start = pi/2)+
  theme_void()+
  labs(title = "Prop of Cars by Cylinder")+
  geom_text(label = label, position = position_stack(vjust = .5),
            color = "black", size = 10)+
  theme(
    plot.background = element_rect(fill = "black"),
    plot.title = element_text(color = "white", hjust = .5),
    legend.text = element_text(color = "white"),
    legend.title = element_text(color = "white", hjust = .5),
    legend.background = element_rect(color = "white", fill = "black")
  )+
  scale_fill_manual(
    values = c(
      "4_cyl" = "white",
      "6_cyl" = "blue",
      "8_cyl" = "red"
    )
  )
  

ggsave("pie_chart.png", plot = x, bg = "black")

Note that we had to assign the plot to a variable (x in this case) before calling the ggsave function. Also note that we used the bg argument inside of the save function to save set the white margins to the same color of our background. Basically, whatever you put in "bg =" should be the same color you used in plot.background. Once we correctly save the chart, the finished output will look like this with no annoying border. 😊

pie_chart.png

Summary

As I indicated in the first tutorial, pie charts are one of the more difficult charts to create using ggplot. However, my goal with this tutorial was to give you the basics of what you need to know to start creating pie charts and adding some of your own customizations. Feel free to leave any questions or comments. As always, thanks so much for reading and I hope you enjoy the rest of your day!



0
0
0.000
4 comments
avatar

Awesome! the "bg" argument completely new to me, thank you, those borders are very anoying. I personally prefer the legend without margins

(legend.background = element_rect(color = NA, fill = "black")

but that´s just preference. Also, I recomend not to use the same word for arguments and objects in the code, is very confusing for people new to the matter (like me) 😅😅.
Thank you for sharing
$wine

0
0
0.000
avatar

That's a good point about not using the same word for arguments and objects. I've confused myself so many times when I'm writing a longer block of code and can't remember what is supposed to be what.

0
0
0.000
avatar

Indeed, and yet it is so common, most sample code I´ve found arround uses repeated words!

0
0
0.000