课程: R for Data Science: Lunch Break Lessons

Pie chart

- [Instructor] The R programming language comes with a whole selection of different kind of graphs and charts. Building a pie chart is one of them, and let's take a look at how to do that. The command is simple, pie, and then you feed it a table of data, so we'll use the table command, and to table we'll feed ChickWeight. And let's graph the Diet component of ChickWeight, and there we have a very simple pie chart, it's that easy. Now there are thing that we can change. Let's go ahead and label the segments. We'll start by copying the command we just created, and pasting it down here. Now let's make an addition to it. Let's put in labels. Once I've typed in a vector of labels, I can run the pie command. This time I'll see the same graph, only this time labeled with the labels that I put in. Now if I don't like the colors that I'm using in this pie chart, I can change it to gradients. To do that, I'll copy and paste the command I previously used, insert a comma and a return, and type in the density. And for density, I want it to come up with four different values, so to do that I'll type in a starting density of 10, and I'm gonna multiply that by the range of one to four. So I'm actually asking for a density of 10, then 20, then 30, then 40. I'll also change the angle of the density, and you'll see what I mean by that in a second. And again, I'm going to start with a 100, and then multiply it by one, then two, then three, then four. When I run this command, you'll see that the colors have changed to angled lines with different densities. So that's pie, it's a built in command to the R language, and provides you with really simple graphs.

内容