The Geometric Objects Layer
00:00 After looking at data and aesthetics, you’ll now learn about the third important layer in plotnine, which is geometric objects. These tell plotnine how to display your data, how it’s supposed to draw it onto the graph.
from plotnine import ggplot, for the data layer,
aes for the aesthetics layer, and then
geom_point, for example, for the geometric objects layer. Now I said, “for example” because there’s a bunch of those that all started with
geom_ and they encode the different geometric objects that you want Python to draw for you.
00:55 So we’ll look at this one first, but I’m also going to show you some other ones after that. And let me introduce you to a different way of writing this that is common when you’re using plotnine, which is not putting everything into one line, but splitting it up in lines by the layers that you’re actually creating.
So if you make these brackets and open up these round parentheses in Python, then you can chain different commands together. So, you would start with the
ggplot() layer, the data, where you pass in the data. Then on a new line, the next layer is going to be aesthetics, where you do the mappings from
x to “class” and
01:45 If you write it like this, it kind of splits it up nicely. You can see each layer separately. You know this is the data layer, this is the aesthetics layer, and this is now the geometric objects layer. Now, when you execute this, you can see a nicely, fully-fledged graph that displays the information that you want it to display.
02:03 So you have the mappings of aesthetics, highway efficiency here on the left, on the y-axis, and the class of cars on the x-axis. And now you have points drawn for each of the data points at the specific points on the graph where the data is located.
02:26 Now, the cool thing about these is that you can easily swap them out. So without changing anything about the data layer or the aesthetics layer, you could use a different layer for displaying the different geometric objects.
And this is the cool thing about this layered grammar of graphics, that it splits up the ideas of what makes up a graphic into these different layers. It says the data is one thing. And in this case, like for these three graphs, the data always stays the same. Now, the aesthetics, the mappings, in this case, also, you want to stick with the same things. You want to keep mapping
03:56 But I want you to take away this idea that by splitting up the data in these different layers, it gives you a lot of flexibility of what you want to change out. For example, here, I could change out, instead of changing out the geometric objects, I could also go in here and change the aesthetic mappings.
04:22 I don’t need to change anything about the rest, and I get a different graph that encodes now what’s the fuel efficiency when the cars drive in the city. So, the layered grammar of graphics gives you this flexibility by splitting up the ideas of what makes up a graphic into separate parts that you can then adapt individually from each other.
04:43 And that pretty much sums up the main ideas about the grammar of graphics and about the three main layers that you need to work with when you’re using plotnine. In the next couple of lessons, you will learn about some additional layers that exist and that you can also adapt if you want to, but often you don’t have to because the defaults are pretty good.
Become a Member to join the conversation.