|
| 1 | +--- |
| 2 | +title: "Additional plotting tools" |
| 3 | +format: |
| 4 | + html: |
| 5 | + number-sections: true |
| 6 | + embed-resources: true |
| 7 | +execute: |
| 8 | + kernel: dataviz |
| 9 | + echo: true |
| 10 | + fig-height: 3 |
| 11 | + fig-width: 4 |
| 12 | + warning: false |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +```{python} |
| 17 | +#| include: false |
| 18 | +
|
| 19 | +import pandas as pd |
| 20 | +import numpy as np |
| 21 | +from plotnine import * |
| 22 | +import matplotlib.pyplot as plt |
| 23 | +from IPython.display import display, Image |
| 24 | +import matplotlib |
| 25 | +
|
| 26 | +ind = pd.read_csv('../../extdata/CPI_HDI.csv').drop(columns=['Unnamed: 0']) |
| 27 | +
|
| 28 | +# Plot theme similar to the original R `mytheme` |
| 29 | +mysize = 9 |
| 30 | +mytheme = theme( |
| 31 | + axis_title = element_text(size=mysize), |
| 32 | + axis_text = element_text(size=mysize), |
| 33 | + legend_title = element_text(size=mysize), |
| 34 | + legend_text = element_text(size=mysize), |
| 35 | + ) + theme_minimal(base_size=mysize) |
| 36 | +
|
| 37 | +np.random.seed(0) |
| 38 | +``` |
| 39 | + |
| 40 | +## Plotting themes |
| 41 | + |
| 42 | +Themes control non-data parts of your plots, such as: |
| 43 | + |
| 44 | +* Overall appearance |
| 45 | +* Axes |
| 46 | +* Plot title |
| 47 | +* Legends |
| 48 | + |
| 49 | +They control the appearance of your plots (size, color, position) but not how the data is represented. |
| 50 | +One example is `theme_bw` which provides a white background. |
| 51 | + |
| 52 | +```{python} |
| 53 | +#| fig-align: center |
| 54 | +
|
| 55 | +(ggplot(ind, aes('CPI', 'HDI', color='region')) + |
| 56 | + geom_point() + |
| 57 | + theme_bw()) |
| 58 | +``` |
| 59 | + |
| 60 | +Other complete themes are: `theme_classic`, `theme_minimal`, `theme_light`, `theme_dark`. |
| 61 | + |
| 62 | +## Axes |
| 63 | + |
| 64 | +The appearance of the axis titles can be controlled with the global variable `axis_title`, |
| 65 | +or independently for x and y using `axis_title_x` and `axis_title_y`. |
| 66 | + |
| 67 | +```{python} |
| 68 | +#| fig-align: center |
| 69 | +(ggplot(ind, aes('CPI', 'HDI', color='region')) + |
| 70 | + geom_jitter() + |
| 71 | + theme(axis_title=element_text(size=20, color='red'))) |
| 72 | +``` |
| 73 | + |
| 74 | +Similar for `axis_text`: |
| 75 | +```{python} |
| 76 | +#| fig-align: center |
| 77 | +(ggplot(ind, aes('CPI', 'HDI', color='region')) + |
| 78 | + geom_jitter() + |
| 79 | + theme(axis_text=element_text(size=20, color='blue'))) |
| 80 | +``` |
| 81 | + |
| 82 | +### Axis elements |
| 83 | + |
| 84 | +The axis elements control the appearance of the axes: |
| 85 | + |
| 86 | +Element | Setter | Description |
| 87 | +--------------------|-------------------|--------------------------- |
| 88 | +axis_line | `element_line()` | line parallel to axis (hidden in default themes) |
| 89 | +axis_text | `element_text()` | tick labels |
| 90 | +axis_text_x | `element_text()` | x-axis tick labels |
| 91 | +axis_text_y | `element_text()` | y-axis tick labels |
| 92 | +axis_title | `element_text()` | axis titles |
| 93 | +axis_title_x | `element_text()` | x-axis title |
| 94 | +axis_title_y | `element_text()` | y-axis title |
| 95 | +axis_ticks | `element_line()` | axis tick marks |
| 96 | +axis_ticks_length | `unit()` | length of tick marks |
| 97 | + |
| 98 | +## Plot title |
| 99 | + |
| 100 | +The appearance of the plot's title can be controlled with the variable `plot_title`. |
| 101 | +```{python} |
| 102 | +#| fig-align: center |
| 103 | +(ggplot(ind, aes('CPI', 'HDI', color='region')) + |
| 104 | + geom_jitter() + |
| 105 | + ggtitle('CPI vs HDI') + |
| 106 | + theme(plot_title=element_text(size=20, weight='bold'))) |
| 107 | +``` |
| 108 | + |
| 109 | +## Legend |
| 110 | + |
| 111 | +The appearance of the legend can be controlled with `legend_text` and `legend_title`. |
| 112 | +```{python} |
| 113 | +#| fig-align: center |
| 114 | +base = (ggplot(ind, aes('CPI', 'HDI', color='region')) + |
| 115 | + geom_jitter()) |
| 116 | +(base + theme( |
| 117 | + legend_text=element_text(size=15), |
| 118 | + legend_title=element_text(size=15, weight='bold'), |
| 119 | + figure_size=(5,3))) |
| 120 | +``` |
| 121 | + |
| 122 | +The legend elements control the appearance of all legends. You can also modify the appearance of individual legends by modifying the same elements in `guide_legend()` or `guide_colorbar()`. |
| 123 | + |
| 124 | +Element | Setter | Description | |
| 125 | +--------------------|---------------------------|---------------------------------------------| |
| 126 | +legend_background | `element_rect()` | legend background | |
| 127 | +legend_key | `element_rect()` | background of legend keys | |
| 128 | +legend_key_size | `unit()` | legend key size | |
| 129 | +legend_key_height | `unit()` | legend key height | |
| 130 | +legend_key_width | `unit()` | legend key width | |
| 131 | +legend_margin | `unit()` | legend margin | |
| 132 | +legend_text | `element_text()` | legend labels | |
| 133 | +legend_text_align | 0--1 | legend label alignment (0 = right, 1 = left)| |
| 134 | +legend_title | `element_text()` | legend name | |
| 135 | +legend_title_align | 0--1 | legend name alignment (0 = right, 1 = left) | |
0 commit comments