For more information on concepts covered in this lesson, you can check out the following resources:
The Scales Layer
00:00 In this lesson, you’re learning about another layer inside of your layered grammar of graphics, and these are the scales and how you can edit those if you want to make your graph more user-friendly without actually changing the data itself. You can do that by improving labeling, and you’re going to also look at an example of reshaping the x values for one of the datasets.
to understand what this dataset is about. Now, if you run into something like this, that the column names—
pce, for example—aren’t very meaningful and you kind of, like, need to understand a little bit about your dataset before you can start plotting so that you know which columns you want to choose. But also, of course, you can learn things about a dataset by plotting it.
00:55 That’s a big point of this exploration that you’re doing using plotnine. But if you want to know more about these datasets, especially the included ones, you can head over to the plotnine documentation, and they have descriptions on each of the included datasets.
01:10 So here, you would see plotnine.data.economics. That’s the one that you just loaded. and it shows US economic time series, so development of economics in the US from a certain time to a certain time. And pce, for example, means personal consumption expenditures, in billions of dollars. Okay.
02:19 Then, I can call this line plot and see what does plotnine give you by default. So you see, it maps the aesthetics onto the scales here. It gives it a name and it plots the development of population over time in the US from this dataset. But these scales here aren’t that meaningful.
02:39 The title here isn’t that great. And it might be interesting to see, how did the population change? Not with dates here, but maybe amount of years, maybe, like decades we could put in here instead and it would maybe be more meaningful.
So, you can add this layer in here and give the plot a title. I’m going to call this
"US Population over time", for example. And then let’s also give a better title to the y-axis here. I’m going to say
y should be
"Population" instead. Let’s look at this.
03:39 And this already looks a little better, you know? You give the context of what is this plot about and give a better name to the y-axis. And then you also said that it could be nice to see from the start, from 1970, in decades, how did this population change? And just change, maybe, the labels that you have down here for the tick marks.
04:32 and then run this again. And then you can see that, again, the labeling down here on your x-axis changed, and it didn’t just add the title as it did with the function that you used before, but it also changed the ticks here.
04:47 So it’s saying, now, this is 1970, so this gives a 0 here. It starts somewhere in 1967, the data, and 1970 is considered year zero, and then it moves 10 years, 20 years, 30 years, 40 years forward.
05:13 And this is what you can do with the scales layer. As you can see, a lot of this is taken care of automatically with reasonable defaults, which is something I’ve mentioned a lot throughout this course, but let’s compare these two again. Without defining anything for the scales layer, you still get a readable graph from that.
05:30 So you often don’t need to do anything about it, but if you want to, you can, and you can make your graphs even better with that. That’s all about scales, and in the next lesson, you’re going to learn about coordinate systems.
Become a Member to join the conversation.