 It's LinkedIn Learning author Monica Wahee with today's data science makeover. Watch while Monica Wahee demonstrates how to use the package ggplot2 to make a pie chart in R. Hi everyone! Today we are going to use the ggplot2 package to make a pie chart. You will see this code here which is actually a long file of code that you can get off of GitHub. The pie chart code is the first code on there. Actually I took a video of myself running this code just to have a video to put in the background of blog posts and things where I need a video running. I actually feel kind of special about this particular pie chart I demonstrated because the data are from a publication I made with one of my favorite colleagues. You can go to the blog post I linked you to and read about that. You might think a pie chart is pretty simple you know compared to time series charts and more complicated plots but actually pie charts are not that straightforward in ggplot2. I'll show you. Let's start by running this code up here. This will read in our data set which I called pie chart underscore df and also call up the ggplot2 library. Okay let's start by looking at our data frame. I'll run our data frame which as I said is called pie chart underscore df. So here's the data frame. It has two columns. In the first column it has a proportion and then the second column is called type and has different types of media assignments you can have in college. Like for blog you could be assigned to write a blog post or like the next one digital storytelling you could be assigned to tell a digital story and so on. So we had blog, digital storytelling, make an e-portfolio, make a podcast and by far the most popular one make a video. My colleague and I were reviewing studies of deeper learning approaches in higher education and this was the distribution of studies we found where they assigned media to be made. So this distribution is what we are making a pie chart of. Let's go back to our code. So actually before we go too far let me point out this color vector. See this vector? It's a character vector called pie underscore colors. It has a list of mapped our colors in it. Actually let me point out that it has exactly five colors. One for each type of media we just looked at. We are going to use this vector in our ggplot2 programming to color the pieces of our pie. Let me just run this and create the vector. Okay now that is in memory. Let's tackle this ggplot code. So let's cut to the chase. The main weird thing about this code is that we don't call any shape called geome underscore pie or geome underscore circle and the weirdness starts on the first line. Look at our aes command. There's no x in it. I just put two quotes for the x. We are not choosing one. y is our proportion column and we set fill to type for the media type. But then look at the next two lines. See that? We are calling geome underscore bar the way you would do in a bar plot. I set the width and the size to one, the color to white, and the stat to identity. Then, and this is really important because this is what makes it a pie. On the next line I have chord underscore polar with the argument of just y in quotes. That is what makes it a pie. Here let me show you. I'll run the code just through geome underscore bar and show you what it does. See that? It's just one big fat stacked bar. But then let's see what happens when I rerun the code this time including the next line with the chord underscore polar y command in it. Okay, now we finally have our circle. But there's still cleanup to do. We have to apply our custom colors as you'll see and we need to do something about those labels and I'd prefer a less cluttered theme. Okay, let's go back to our code. Let's look at the rest of this plot code. The next line is a geome underscore text line. This uses a paste command to format the proportions as rounded percentages with a percent sign after them and put them as data labels. Note the position command. I said position equals position underscore stack and then in parentheses I say vjust equals 0.5. This is a way of adjusting the data label so it has shifted just a little and doesn't overlap things. Next we have the lab command. Notice all those nulls. That's how I turned off the labels I didn't want that we just saw a moment ago. And that's how I made the title say what I wanted. The guides command formats the legend. For whatever reason I wanted the legend to be presented in the reverse order. So that's what that reverse equals true is. The next line scale underscore fill underscore manual replaces those awful default colors with our color vector called pi underscore colors. Next theme underscore classic applies a minimalist theme to suppress all that clutter like that gray background you saw. And then we use a theme command to further suppress some items from the display and to color the title. Okay let's run this new improved code. I promise the pi will look a lot more appetizing this time. Now that's classy. That's one attractive pie chart. Thank you for watching this data science makeover with LinkedIn Learning author Monica Wahee. Remember to check out Monica's data science courses on LinkedIn Learning. Click on the link in the description. Thank you for watching. If you like the video then please hit the like button. Also I invite you to look around my channel and if you like what you see subscribe. I hope this video finds you well and you are having a super day.