3D Pie Chart in One Line of Code?

As I have demonstrated in the previous tutorials, it is definitely possible to make a pie chart using the ggplot package in the R programming language. But let's be honest, it can be a little bit of a pain. There's no default command to create a pie chart. Instead, you have to create a bar chart and do some transformations, and it can get really complicated. So in today's post, I'm going to show you how to make a pie chart in one simple line of code.

Getting Started

Instead of using ggplot, we're going to use a different package called plotrix. Simply install this package, then pull it into your workspace using the code below. NOTE: I'm going to assume some creative liberties here, and I'm not counting installing the background package in my one line of code limit.

install.packages("plotrix")
library (plotrix)

Once you have the package pulled into your workspace, creating a pie chart requires only a single line of code. Ready to see how simple it is?

pie3D(x = c(1,2,3))

This pie chart literally requires only a single line of code. We call the pie3d command, and then we pass in x, which is a vector of the different counts of each category. For example, let's suppose we have a data set with one red car, two green cars, and three blue cars.

image.png

It's a Start...But It Needs Some Work

There you go! As promised, I showed you how to create a 3d pie chart in a single line of code. Now, obviously, we need to spice this pie chart up a little bit, but the good thing is that we can do all of the customization within a single function. There's no need to use the plus sign and keep adding multiple layers of geometries like we do with ggplot. To make the plot a little more interesting, I'm also going to create two vectors of categories and counts to pass into the pie3D argument instead of manually typing all the values directly into the function.

Note that I have provided a brief description of what each argument does directly inside the code block using the comment function. Also note that the par arguments are not directly inside the pie3D command.

my_categories <- c("4_cyl","6_cyl", "8_cyl")
my_counts <- c(14,7,14)

par(bg = "black", col.main = "white")  #This changes the background and title color

pie3D(x = my_counts, labels = my_categories, main = "My 3D Pie Chart",

      height = .5, #Grow or shrink the height of the pie chart. Bigger # -> taller

      theta = 1,   #Rotates the pie chart forward or backwards

      edges = 5,   # Tells R how many distinct line segments to use for each slice.

      col = c("blue","green","purple"),  #Manually specifying the color of each slice
                     #NOTE: The order should be the same as your category vector
      explode = .1,      # Breaks the pie chart apart. Bigger # -> more space

      radius = 1,         # Bigger # -> bigger pie chart

      #start = 20,        # Rotates the pie chart -> I don't use that much

      border = "black",   # Changes border color of the pie chart

      labelcol = "red",   # Changes the label colors

      shade = .8,     # Shades the walls of the pie chart. Smaller # >- darker

      #mar = c(0,5,0,5)   # Adjusts margins. 
      
      )

image.png

Is It Better Than ggplot??

If plotrix makes it so easy to create a 3D pie chart, then why did I spend the first two lessons talking about the convoluted way to create a pie chart using ggplot? Well, there are a couple of reasons. The first is that ggplot offers many more options for detailed customization. Once we move beyond customizing some basic options with plotrix, things get pretty complicated pretty quick. On top of that, the benefit of ggplot is that everything we learn as far as customizing one plot transfers to all of the other different types of plots. Even if one specific type of plot is harder to create in ggplot, I still think the benefits of learning a single package outweigh the advantages of learning a new package every single time we want to create a different plot.

As always, thanks so much for taking the time to read the article!

PS:
I've had an absolutely incredible time making these tutorials and sharing them with everyone here, but school is about to start back up, so the pace of the tutorials might slow down a little bit.



0
0
0.000
3 comments
avatar
(Edited)

Absolutely mesmerizing the basic options of plotrix, customizations, bit less easy. However, is there a way to create a 2d pie chat en Plotrix? I imagine it is, but since I've never used it I wanted to ask. I don't like 3d charts, looks very nice, but data visualization often get not so clear.
Thanks
$wine

0
0
0.000
avatar

I'm not aware of any 2d pie chart functionality in plotrix. To be fair, I mostly use ggplot, so plotrix might have the function, and I just don't know about it.

0
0
0.000