-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggstream-labels.Rmd
More file actions
99 lines (69 loc) · 2.12 KB
/
ggstream-labels.Rmd
File metadata and controls
99 lines (69 loc) · 2.12 KB
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
---
title: "Working with a `ggstream` plot"
author: JR Ferrer-Paris (https://github.com/jrfep)
date: 2021-06-21
output:
pdf_document:
fig_width: 4
fig_height: 3
html_document: default
editor_options:
chunk_output_type: console
---
We want to test the package `ggstream` and work with the function `geom_stream_labels`.
Check this post: https://r-charts.com/evolution/ggstream/
Package documentation: https://rdrr.io/github/davidsjoberg/ggstream/man/geom_stream.html
## Required packages
```{r}
# install.packages("remotes")
# remotes::install_github("davidsjoberg/ggstream")
library(ggstream)
# install.packages("ggplot2")
library(ggplot2)
```
## Reproducible data
Run example with a default dataset:
```{r}
ggplot(blockbusters, aes(x = year, y = box_office, fill = genre)) +
geom_stream()
```
## Solution
We can save the plot to an object and then add/change elements, for example:
### Add labels
```{r}
my.plot <- ggplot(blockbusters, aes(x = year, y = box_office, fill = genre)) +
geom_stream()
my.plot + geom_stream_label(aes(label = genre))
```
### Change type of stream plot
This will not work with a different type of plot
```{r}
my.plot <- ggplot(blockbusters, aes(x = year, y = box_office, fill = genre))
my.plot + geom_stream(type='ridge') + geom_stream_label(aes(label = genre))
```
unless...
```{r}
my.plot + geom_stream(type='ridge') + geom_stream_label(aes(label = genre),type='ridge')
```
Check another type
```{r}
my.plot + geom_stream(type='proportional') + geom_stream_label(aes(label = genre),type='proportional')
```
### Change colors and add border
```{r}
cols <- c("#FFB400", "#FFC740", "#C20008", "#FF020D", "#13AFEF")
my.plot <- my.plot + geom_stream(type='ridge',color=1,lwd=0.25) + geom_stream_label(aes(label = genre),type='ridge')
my.plot + scale_fill_manual(values = cols)
```
### Change theme
Adapt the colors for a different theme:
```{r}
## you could use RColorBrewer::brewer.pal(5,"Pastel")
cols <- c("#FBB4AE", "#B3CDE3", "#CCEBC5", "#DECBE4", "#FED9A6")
my.plot + scale_fill_manual(values = cols) + theme_dark()
```
## Debugging info
Check the R session info:
```{r}
sessionInfo()
```