@@ -89,7 +89,7 @@ Now we can apply the `theme_wb` theme to the plot. This will change the used fon
89
89
barchart + theme_wb()
90
90
```
91
91
92
- The ` wb_theme() ` can be added to any ggplot visualization. But the World Bank data visualization style calls for some bar chart specific styling options. We can apply those by setting by setting the ` chartType ` parameter to "bar" in the theme function:
92
+ The ` wb_theme() ` can be added to any ggplot visualization. But the World Bank data visualization style calls for some bar chart specific styling options. We can apply those by setting by setting the ` chartType ` parameter to "bar" in the theme function:
93
93
94
94
``` {r, fig.dim = c(8,4), dpi = 300, out.width = "100%"}
95
95
barchart + theme_wb(chartType = "bar")
@@ -132,7 +132,7 @@ print(scatterdata)
132
132
```
133
133
With this data, we can create a default ggplot scatterplot.
134
134
135
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
135
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
136
136
scatterplot <- ggplot(scatterdata, aes(NY.GDP.PCAP.PP.CD, SP.DYN.LE00.IN, fill = region_iso3c)) +
137
137
geom_point() +
138
138
labs(
@@ -147,33 +147,33 @@ Notice that because you ran the `theme_wb()` function before, the default shape
147
147
148
148
Applying ` theme_wb ` to this plot gives you this:
149
149
150
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
150
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
151
151
scatterplot + theme_wb()
152
152
```
153
153
154
154
Like for bar charts, the World Bank data visualization style has some specific styling for scatter plots:
155
155
156
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
156
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
157
157
scatterplot + theme_wb(chartType = "scatter")
158
158
```
159
159
160
160
Whenever a continuous scale is present, you can include a line to indicate the zero value on the axis. When zero was not included on the axis initially, the scale will extend to include it.
161
161
162
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
162
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
163
163
scatterplot + theme_wb(chartType = "scatter", addXZeroLine = TRUE, addYZeroLine = TRUE)
164
164
```
165
165
166
166
The wbplot package includes continuous and discrete color scales for the World Bank data visualization color palettes. In this case we should use the ` scale_fill_wb_d ` to apply the default discrete color palette to the regions.
167
167
168
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
168
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
169
169
scatterplot +
170
170
theme_wb(chartType = "scatter", addXZeroLine = TRUE) +
171
171
scale_fill_wb_d(name = "Region")
172
172
```
173
173
174
174
Each World Bank also has a color assigned to it in the ` region ` palette, so we can map the region code directly to the corresponding color.
175
175
176
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
176
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
177
177
scatterplot +
178
178
aes(fill = tolower(region_iso3c)) +
179
179
theme_wb(chartType = "scatter", addXZeroLine = TRUE) +
@@ -182,7 +182,7 @@ scatterplot +
182
182
183
183
Income levels also have their own palette, called "income".
184
184
185
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
185
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
186
186
scatterplot +
187
187
aes(fill = tolower(income_level_iso3c)) +
188
188
theme_wb(chartType = "scatter", addXZeroLine = TRUE) +
@@ -191,25 +191,26 @@ scatterplot +
191
191
192
192
Alternatively, we can map the fill aesthetic to a numeric variable, and use the continuous color scale to apply a World Bank continuous color palette.
193
193
194
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
194
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
195
195
scatterplot +
196
196
aes(fill = NY.GDP.PCAP.PP.CD) +
197
197
theme_wb(chartType = "scatter", addXZeroLine = TRUE) +
198
198
scale_fill_wb_c(palette = "seq", name = "GDP/capita")
199
199
```
200
200
201
201
Binned color scales are also provided:
202
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
202
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
203
203
scatterplot +
204
204
aes(fill = NY.GDP.PCAP.PP.CD) +
205
205
theme_wb(chartType = "scatter", addXZeroLine = TRUE) +
206
206
scale_fill_wb_b(palette = "divPosNeg", name = "GDP/capita", n.breaks = 8)
207
207
```
208
+
208
209
## Styling a line chart
209
210
210
211
Let's prepare some time series data and make a ggplot line chart.
211
212
212
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
213
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
213
214
life.series <- filter(life.expectancy, iso3c %in% c("USA", "CHN", "IND", "DEU", "RUS", "IDN", "JPN"))
214
215
215
216
linechart <- ggplot(life.series, aes(x = date, y = SP.DYN.LE00.IN, color = iso3c)) +
@@ -221,7 +222,7 @@ linechart
221
222
222
223
Applying the general ` wb_theme ` and the line chart specific styling to it, together with the World Bank discrete color scale:
223
224
224
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
225
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
225
226
linechart +
226
227
theme_wb(chartType = "line", addYZeroLine = TRUE) +
227
228
scale_color_wb_d() +
@@ -230,7 +231,7 @@ linechart +
230
231
231
232
We can give more context by providing the data for all countries in the background.
232
233
233
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
234
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
234
235
linechart +
235
236
theme_wb(chartType = "line", addYZeroLine = TRUE) +
236
237
scale_color_wb_d() +
@@ -252,7 +253,7 @@ Here you can see that all colors of the World Bank data visualization style are
252
253
253
254
Finally, let's create a beeswarm plot and style it. To plot a beeswarm plot, we use the ggbeeswarm package.
254
255
255
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
256
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
256
257
lifeexp.22 <- filter(life.expectancy, date == 2022) %>%
257
258
left_join(countries, by = "iso3c") %>%
258
259
filter(income_level_iso3c != "INX") %>%
@@ -270,7 +271,7 @@ beeswarm <- ggplot(lifeexp.22, ggplot2::aes(x = SP.DYN.LE00.IN, y = income_level
270
271
beeswarm
271
272
```
272
273
273
- ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%"}
274
+ ``` {r, fig.dim = c(8,6), dpi = 300, out.width = "100%", warning = FALSE }
274
275
beeswarm +
275
276
theme_wb(chartType = "beeswarm", addYAxisTitle = TRUE) +
276
277
scale_fill_wb_d(palette = "income") +
0 commit comments