-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts_barcharts.Rmd
executable file
·126 lines (100 loc) · 2.68 KB
/
charts_barcharts.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: "Charts: Barcharts"
output:
html_document:
toc: true
toc_float:
collapsed: false
includes:
before_body: [includes/include_header.html, includes/include_header_navpage.html]
editor_options:
chunk_output_type: console
---
# What is a barchart?
Barcharts are very flexible visualisations that allow viewers to quickly compare the relative magnitude/ranking of multiple categories of data. By using grouped or stacked barcharts it is also possible to compare subcategories within each category.
Required Data:
- Categories: For instance, class of animal e.g. mammal or reptile
- Values: For instance, number of animals of each class
Additional Data/Options:
- Subcategories: For instance, species of animal within a class e.g. snake and iguana
- Group/Stacking Type: Should sub-categories be grouped together or stacked?
Below are three minimal examples using `R` and the `highcharter` library:
<!--html_preserve-->
<div class="row">
<div class="col-md-4">
```{r basic_barchart, echo=FALSE, message=FALSE, warning=FALSE}
library("highcharter")
library("tidyverse")
my_data <- tribble(
~category, ~subcategory, ~value,
"Mammal", "human", 3,
"Mammal", "non-human", 4,
"Reptile", "snake", 6,
"Reptile", "iguana", 8
)
my_data %>%
hchart(
type = "bar",
hcaes(
x = category,
y = value
)
) %>%
hc_size(width = "250px", height = "250px") %>%
hc_title(text = "Normal barchart",
style = list(fontSize = 12))
```
</div>
<div class="col-md-4">
```{r grouped_barchart, echo=FALSE, message=FALSE, warning=FALSE}
library("highcharter")
library("tidyverse")
my_data <- tribble(
~category, ~subcategory, ~value,
"Mammal", "human", 3,
"Mammal", "non-human", 4,
"Reptile", "snake", 6,
"Reptile", "iguana", 8
)
my_data %>%
hchart(
type = "bar",
hcaes(
x = category,
y = value,
group = subcategory
)
) %>%
hc_size(width = "250px", height = "250px") %>%
hc_title(text = "Grouped barchart",
style = list(fontSize = 12))
```
</div>
<div class="col-md-4">
```{r stacked_barchart, echo=FALSE, message=FALSE, warning=FALSE}
library("highcharter")
library("tidyverse")
my_data <- tribble(
~category, ~subcategory, ~value,
"Mammal", "human", 3,
"Mammal", "non-human", 4,
"Reptile", "snake", 6,
"Reptile", "iguana", 8
)
my_data %>%
hchart(
type = "bar",
hcaes(
x = category,
y = value,
group = subcategory
)
) %>%
hc_size(width = "250px", height = "250px") %>%
hc_plotOptions(series = list(stacking = "percent")) %>%
hc_title(text = "Stacked percentage barchart",
style = list(fontSize = 12))
```
</div>
</div>
<!--/html_preserve-->