-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.Rmd
100 lines (81 loc) · 2.97 KB
/
examples.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
title: "Examples"
date: "`r Sys.Date()`"
scctemplate:
header:
site_branding: "Suffolk County Council"
navigation:
breadcrumb_trail:
- href: "index.html"
text: "Home"
- text: "Examples"
toc:
sticky: false
numbered: false
---
## Make a barchart
```{r helper_example, warning = FALSE, message = FALSE}
library(ggplot2)
library(gapminder)
library(dplyr)
library(sccthemes)
barchart_data <- gapminder |>
filter(year == 2007 & continent == "Europe") |>
arrange(desc(gdpPercap)) |>
head(5)
scc_barchart(
barchart_data,
x = "country",
y = "gdpPercap",
title = "Europe's Richest Countries",
subtitle = "GDP per Capita, 2007"
)
```
## Make a piechart
Example on how to make a pie chart, but [why you shouldn't](https://www.data-to-viz.com/caveat/pie.html)
```{r piechart, warning = FALSE, message = FALSE}
library(sccthemes)
library(gapminder)
library(dplyr)
asia_pop <- gapminder |>
filter(year == 1997 & continent == "Asia") |>
select(country, lifeExp, pop, gdpPercap) |>
top_n(5)
asia_pop$country <- forcats::fct_drop(asia_pop$country)
scc_piechart(
asia_pop,
asia_pop$gdpPercap,
asia_pop$country,
title = "GDP per Capita contribution percentage by county",
subtitle = "Five largest countries only"
)
```
## Publishing your plots
To publish your work, so will need to save your plot. Saving plots in ggplot2 can be a bit of a hassle, so we're porting bbplot's `finalise_plot()` function. Some adjustments have been made, for example we might not want a logo on every plot. Also, because this package does not just deal with ggplot, I've renamed the function `finalise_ggplot()`, to specify this is for `ggplot2` object only.
Once you have created your plot and are happy with it, you can use `finalise_ggplot()` so that you can look at it outside RStudio. Note that the position of the text and other elements do not render accurately in the RStudio Plots panel, so saving it out and opening up the files give you an accurate representation of how the graphic looks.
Also you might want to use the same plot for different purpose: the size of a plot in a presentation will need to be a different size from one in a report. `finalise_ggplot()` allows for this without having to change the original plot.
```{r finalise_ggplot, warning = FALSE, message = FALSE, fig.show = "hide"}
library(ggplot2)
library(gapminder)
library(dplyr)
library(sccthemes)
barchart_data <- gapminder |>
filter(year == 2007 & continent == "Europe") |>
arrange(desc(gdpPercap)) |>
head(5)
plot_to_save <- scc_barchart(
barchart_data,
x = "country",
y = "gdpPercap",
title = "Europe's Richest Countries",
subtitle = "GDP per Capita, 2007"
)
finalise_ggplot(
plot_to_save,
source = "Jennifer Bryan, 2017, Gapminder R package, https://github.com/jennybc/gapminder",
save_filepath = "inst/plots/publish_plot_example.png",
width_pixels = 225,
height_pixels = 165
)
```
<p align="center"><img src="inst/plots/publish_plot_example.png"></p>