-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-bubblecloud.Rmd
144 lines (122 loc) · 6.84 KB
/
2021-08-04-bubblecloud.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
name: Bubblecloud Plots
permalink: ggplot2/bubblecloud/
description: How to make a Bubblecloud Plots using ggplotly with Plotly.
layout: base
thumnail_github: bubblecloud.png
language: ggplot2
page_type: u-guide
display_as: basic
order: 21
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
### Basic Text Graph
Sources: [International IDEA](https://www.idea.int/data-tools/continent-view/Europe/40?st=par#rep) for national turnout and [European Parliament](https://election-results.eu/turnout/) for European turnout, while regional classifications are based on [EuroVoc](https://publications.europa.eu/en/web/eu-vocabularies/th-concept-scheme/-/resource/eurovoc/100277?target=Browse).
```{r}
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
library(plotly)
p <- recent_turnout %>%
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
geom_text(aes(size=population/3.5, label=abbreviation, colour=region), alpha=1) +
labs(title = "Recent turnout in European Union countries",
x = "Latest legislative or presidential election (whichever had higher turnout)",
y = "May 2019 European Parliament election")
fig <- ggplotly(p)
fig
```
### Overlaid Points
Colour-coding the text itself might present readability issues. Another possible use of geom\_text is to keep the text grey, but overlay it on a coloured point graph.
Adding the *text* option within aes() allows us to control the text that appears when hovering over a point.
```{r}
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
library(plotly)
p <- recent_turnout %>%
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
labs(title = "Recent turnout in European Union countries",
x = "Latest legislative or presidential election (whichever had higher turnout)",
y = "May 2019 European Parliament election")
fig <- ggplotly(p)
fig
```
### Customed Colour and Size Scale
Let's use the LaCroixColoR package to spruce up the colour scheme. In addition, by using scale\_size\_continuous, we can make sure that none of the text is too small.
```{r}
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
library(plotly)
library(LaCroixColoR)
p <- recent_turnout %>%
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
scale_size_continuous(range = c(3, 8)) +
labs(title = "Recent turnout in European Union countries",
x = "Latest legislative or presidential election (whichever had higher turnout)",
y = "May 2019 European Parliament election")
fig <- ggplotly(p)
fig
```
### Adding a regression
Adding a regression line as well as a label. geom\_smooth does not allow for adjusting the transparency of the line (using alpha), which is why stat\_smooth is used here. annotate is used to include a single text label (geom\_text would create one label for every data point, all overlapped with each other).
```{r}
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
library(plotly)
library(LaCroixColoR)
p <- recent_turnout %>%
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
scale_size_continuous(range = c(3, 8)) +
labs(title = "Recent turnout in European Union countries",
x = "Latest legislative or presidential election (whichever had higher turnout)",
y = "May 2019 European Parliament election") +
annotate(geom="text", x=60, y=80, label = paste("European turnout = \n",
round(unname(coef(m)[2]),2),
"x national turnout",
round(unname(coef(m)[1]),1)))
fig <- ggplotly(p)
fig
```
### Customized Formatting
Changed the font of the geom\_text and of the graph (these must be done separately!), corrected the size label, centre-aligned the title.
```{r}
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
library(plotly)
library(LaCroixColoR)
p <- recent_turnout %>%
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1, family="Fira Sans") +
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
scale_size_continuous(range = c(3, 8)) +
labs(title = "Recent turnout in European Union countries",
x = "Latest legislative or presidential election (whichever had higher turnout)",
y = "May 2019 European Parliament election",
size = "") +
annotate(geom="text", x=60, y=80, label = paste("European turnout = \n",
round(unname(coef(m)[2]),2),
"x national turnout",
round(unname(coef(m)[1]),1))) +
theme(plot.title = element_text(hjust = 0.5)) +
guides(size=guide_legend(""), fill = FALSE) +
theme(text = element_text(family = 'Fira Sans'))
fig <- ggplotly(p)
fig
```