@@ -1247,12 +1247,10 @@ and allows us to answer our initial questions:
1247
1247
"Are the seven continents Earth's largest landmasses?"
1248
1248
and "Which are the next few largest landmasses?".
1249
1249
However, we could still improve this visualization
1250
- by organizing the bars by landmass size rather than by alphabetical order
1251
- and by coloring the bars based on whether they correspond to a continent.
1252
- The data for this is stored in the ` landmass_type ` column.
1253
- To use this to color the bars,
1250
+ by coloring the bars based on whether they correspond to a continent, and
1251
+ by organizing the bars by landmass size rather than by alphabetical order.
1252
+ The data for coloring the bars is stored in the ` landmass_type ` column, so
1254
1253
we set the ` color ` encoding to ` landmass_type ` .
1255
-
1256
1254
To organize the landmasses by their ` size ` variable,
1257
1255
we will use the altair ` sort ` function
1258
1256
in the y-encoding of the chart.
@@ -1262,18 +1260,37 @@ This plots the values on `y` axis
1262
1260
in the ascending order of ` x ` axis values.
1263
1261
This creates a chart where the largest bar is the closest to the axis line,
1264
1262
which is generally the most visually appealing when sorting bars.
1265
- If instead
1266
- we want to sort the values on ` y-axis ` in descending order of ` x-axis ` ,
1267
- we can add a minus sign to reverse the order and specify ` sort="-x" ` .
1263
+ If instead we wanted to sort the values on ` y-axis ` in descending order of ` x-axis ` ,
1264
+ we could add a minus sign to reverse the order and specify ` sort="-x" ` .
1268
1265
1269
1266
``` {index} altair; sort
1270
1267
```
1271
1268
1272
- ``` {code-cell} ipython3
1273
- islands_plot_sorted = alt.Chart(islands_top12).mark_bar().encode(
1274
- x="size",
1275
- y=alt.Y("landmass").sort("x"),
1276
- color=alt.Color("landmass_type")
1269
+ To finalize this plot we will customize the axis and legend labels using the ` title ` method,
1270
+ and add a title to the chart by specifying the ` title ` argument of ` alt.Chart ` .
1271
+ Plot titles are not always required, especially when it would be redundant with an already-existing
1272
+ caption or surrounding context (e.g., in a slide presentation with annotations).
1273
+ But if you decide to include one, a good plot title should provide the take home message
1274
+ that you want readers to focus on, e.g., "The Earth's seven largest landmasses are all continents,"
1275
+ but it could also more general, e.g., "The twelve largest landmasses on Earth."
1276
+
1277
+ Note that
1278
+ For categorical encodings,
1279
+ such as the color and y channels in our chart,
1280
+ it is often not necessary to include the axis title
1281
+ as the labels of the categories are enough by themselves.
1282
+ Particularly in this case where the title clearly states
1283
+ that we are landmasses,
1284
+ the titles are redundant and we can remove them.
1285
+
1286
+ ``` {code-cell} ipython3
1287
+ islands_plot_sorted = alt.Chart(
1288
+ islands_top12,
1289
+ title="The Earth's seven largest landmasses are all continents"
1290
+ ).mark_bar().encode(
1291
+ x=alt.X("size").title("Size (1000 square mi)"),
1292
+ y=alt.Y("landmass").sort("x").title("Landmass"),
1293
+ color=alt.Color("landmass_type").title("Type")
1277
1294
)
1278
1295
```
1279
1296
0 commit comments