Skip to content

Commit 357761a

Browse files
committed
added geom_text
1 parent 522dd52 commit 357761a

File tree

2 files changed

+382
-0
lines changed

2 files changed

+382
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
---
2+
title: geom_text | Examples | Plotly
3+
name: geom_text
4+
permalink: ggplot2/geom_text/
5+
description: How to add text .
6+
layout: base
7+
thumbnail: thumbnail/geom_text.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: basic
12+
order: 11
13+
output:
14+
html_document:
15+
keep_md: true
16+
---
17+
18+
```{r, echo = FALSE, message=FALSE}
19+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
20+
Sys.setenv("plotly_username"="RPlotBot")
21+
Sys.setenv("plotly_api_key"="q0lz6r5efr")
22+
```
23+
24+
### New to Plotly?
25+
26+
Plotly's R library is free and open source!<br>
27+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
28+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
29+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
30+
31+
### Version Check
32+
33+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
34+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
35+
36+
```{r}
37+
library(plotly)
38+
packageVersion('plotly')
39+
```
40+
41+
### Basic Text Graph
42+
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).
43+
44+
```{r, results='hide'}
45+
library(plotly)
46+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
47+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
48+
49+
p <- recent_turnout %>%
50+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
51+
geom_text(aes(size=population/3.5, label=abbreviation, colour=region), alpha=1) +
52+
labs(title = "Recent turnout in European Union countries",
53+
x = "Latest legislative or presidential election (whichever had higher turnout)",
54+
y = "May 2019 European Parliament election")
55+
p <- ggplotly(p)
56+
57+
# Create a shareable link to your chart
58+
# Set up API credentials: https://plot.ly/r/getting-started
59+
chart_link = api_create(p, filename="geom_text/basic-chart")
60+
chart_link
61+
```
62+
63+
```{r echo=FALSE}
64+
chart_link
65+
```
66+
67+
### Overlaid Points
68+
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.
69+
70+
Adding the *text* option within aes() allows us to control the text that appears when hovering over a point.
71+
72+
```{r, results='hide'}
73+
library(plotly)
74+
75+
p <- recent_turnout %>%
76+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
77+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
78+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
79+
labs(title = "Recent turnout in European Union countries",
80+
x = "Latest legislative or presidential election (whichever had higher turnout)",
81+
y = "May 2019 European Parliament election")
82+
p <- ggplotly(p)
83+
84+
# Create a shareable link to your chart
85+
# Set up API credentials: https://plot.ly/r/getting-started
86+
chart_link = api_create(p, filename="geom_text/overlaid-points")
87+
chart_link
88+
```
89+
90+
```{r echo=FALSE}
91+
chart_link
92+
```
93+
94+
### Customed Colour and Size Scale
95+
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.
96+
97+
```{r, results='hide'}
98+
library(plotly)
99+
library(LaCroixColoR)
100+
101+
p <- recent_turnout %>%
102+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
103+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
104+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
105+
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
106+
scale_size_continuous(range = c(3, 8)) +
107+
labs(title = "Recent turnout in European Union countries",
108+
x = "Latest legislative or presidential election (whichever had higher turnout)",
109+
y = "May 2019 European Parliament election")
110+
p <- ggplotly(p)
111+
112+
# Create a shareable link to your chart
113+
# Set up API credentials: https://plot.ly/r/getting-started
114+
chart_link = api_create(p, filename="geom_text/customized-scales")
115+
chart_link
116+
```
117+
118+
```{r echo=FALSE}
119+
chart_link
120+
```
121+
122+
### Adding a regression
123+
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).
124+
125+
```{r, results='hide'}
126+
library(plotly)
127+
library(LaCroixColoR)
128+
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
129+
130+
p <- recent_turnout %>%
131+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
132+
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
133+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
134+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
135+
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
136+
scale_size_continuous(range = c(3, 8)) +
137+
labs(title = "Recent turnout in European Union countries",
138+
x = "Latest legislative or presidential election (whichever had higher turnout)",
139+
y = "May 2019 European Parliament election") +
140+
annotate(geom="text", x=60, y=80, label = paste("European turnout = \n",
141+
round(unname(coef(m)[2]),2),
142+
"x national turnout",
143+
round(unname(coef(m)[1]),1)))
144+
p <- ggplotly(p)
145+
146+
# Create a shareable link to your chart
147+
# Set up API credentials: https://plot.ly/r/getting-started
148+
chart_link = api_create(p, filename="geom_text/add-regression")
149+
chart_link
150+
```
151+
152+
```{r echo=FALSE}
153+
chart_link
154+
```
155+
156+
### Customized Formatting
157+
Changed the font of the geom_text and of the graph (these must be done separately!), corrected the size label, centre-aligned the title.
158+
159+
```{r, results='hide'}
160+
library(plotly)
161+
library(LaCroixColoR)
162+
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
163+
164+
p <- recent_turnout %>%
165+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
166+
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
167+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
168+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1, family="Fira Sans") +
169+
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
170+
scale_size_continuous(range = c(3, 8)) +
171+
labs(title = "Recent turnout in European Union countries",
172+
x = "Latest legislative or presidential election (whichever had higher turnout)",
173+
y = "May 2019 European Parliament election",
174+
size = "") +
175+
annotate(geom="text", x=60, y=80, label = paste("European turnout = \n",
176+
round(unname(coef(m)[2]),2),
177+
"x national turnout",
178+
round(unname(coef(m)[1]),1))) +
179+
theme(plot.title = element_text(hjust = 0.5)) +
180+
guides(size=guide_legend(""), fill = FALSE) +
181+
theme(text = element_text(family = 'Fira Sans'))
182+
p <- ggplotly(p)
183+
184+
# Create a shareable link to your chart
185+
# Set up API credentials: https://plot.ly/r/getting-started
186+
chart_link = api_create(p, filename="geom_text/add-formatting")
187+
chart_link
188+
```
189+
190+
```{r echo=FALSE}
191+
chart_link
192+
```
193+
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
title: geom_text | Examples | Plotly
3+
name: geom_text
4+
permalink: ggplot2/geom_text/
5+
description: How to add text .
6+
layout: base
7+
thumbnail: thumbnail/geom_text.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: basic
12+
order: 11
13+
output:
14+
html_document:
15+
keep_md: true
16+
---
17+
18+
19+
20+
### New to Plotly?
21+
22+
Plotly's R library is free and open source!<br>
23+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
24+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
25+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
26+
27+
### Version Check
28+
29+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
30+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
31+
32+
33+
```r
34+
library(plotly)
35+
packageVersion('plotly')
36+
```
37+
38+
```
39+
## [1] '4.9.0.9000'
40+
```
41+
42+
### Basic Text Graph
43+
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).
44+
45+
46+
```r
47+
library(plotly)
48+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
49+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
50+
51+
p <- recent_turnout %>%
52+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
53+
geom_text(aes(size=population/3.5, label=abbreviation, colour=region), alpha=1) +
54+
labs(title = "Recent turnout in European Union countries",
55+
x = "Latest legislative or presidential election (whichever had higher turnout)",
56+
y = "May 2019 European Parliament election")
57+
p <- ggplotly(p)
58+
59+
# Create a shareable link to your chart
60+
# Set up API credentials: https://plot.ly/r/getting-started
61+
chart_link = api_create(p, filename="geom_text/basic-chart")
62+
chart_link
63+
```
64+
65+
<iframe src="https://plot.ly/~RPlotBot/5778.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
66+
67+
### Overlaid Points
68+
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.
69+
70+
Adding the *text* option within aes() allows us to control the text that appears when hovering over a point.
71+
72+
73+
```r
74+
library(plotly)
75+
76+
p <- recent_turnout %>%
77+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
78+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
79+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
80+
labs(title = "Recent turnout in European Union countries",
81+
x = "Latest legislative or presidential election (whichever had higher turnout)",
82+
y = "May 2019 European Parliament election")
83+
p <- ggplotly(p)
84+
85+
# Create a shareable link to your chart
86+
# Set up API credentials: https://plot.ly/r/getting-started
87+
chart_link = api_create(p, filename="geom_text/overlaid-points")
88+
chart_link
89+
```
90+
91+
<iframe src="https://plot.ly/~RPlotBot/5780.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
92+
93+
### Customed Colour and Size Scale
94+
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.
95+
96+
97+
```r
98+
library(plotly)
99+
library(LaCroixColoR)
100+
101+
p <- recent_turnout %>%
102+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
103+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
104+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
105+
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
106+
scale_size_continuous(range = c(3, 8)) +
107+
labs(title = "Recent turnout in European Union countries",
108+
x = "Latest legislative or presidential election (whichever had higher turnout)",
109+
y = "May 2019 European Parliament election")
110+
p <- ggplotly(p)
111+
112+
# Create a shareable link to your chart
113+
# Set up API credentials: https://plot.ly/r/getting-started
114+
chart_link = api_create(p, filename="geom_text/customized-scales")
115+
chart_link
116+
```
117+
118+
<iframe src="https://plot.ly/~RPlotBot/5782.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
119+
120+
### Adding a regression
121+
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).
122+
123+
124+
```r
125+
library(plotly)
126+
library(LaCroixColoR)
127+
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
128+
129+
p <- recent_turnout %>%
130+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
131+
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
132+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
133+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) +
134+
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
135+
scale_size_continuous(range = c(3, 8)) +
136+
labs(title = "Recent turnout in European Union countries",
137+
x = "Latest legislative or presidential election (whichever had higher turnout)",
138+
y = "May 2019 European Parliament election") +
139+
annotate(geom="text", x=60, y=80, label = paste("European turnout = \n",
140+
round(unname(coef(m)[2]),2),
141+
"x national turnout",
142+
round(unname(coef(m)[1]),1)))
143+
p <- ggplotly(p)
144+
145+
# Create a shareable link to your chart
146+
# Set up API credentials: https://plot.ly/r/getting-started
147+
chart_link = api_create(p, filename="geom_text/add-regression")
148+
chart_link
149+
```
150+
151+
<iframe src="https://plot.ly/~RPlotBot/5774.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
152+
153+
### Customized Formatting
154+
Changed the font of the geom_text and of the graph (these must be done separately!), corrected the size label, centre-aligned the title.
155+
156+
157+
```r
158+
library(plotly)
159+
library(LaCroixColoR)
160+
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
161+
162+
p <- recent_turnout %>%
163+
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
164+
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
165+
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
166+
geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1, family="Fira Sans") +
167+
scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) +
168+
scale_size_continuous(range = c(3, 8)) +
169+
labs(title = "Recent turnout in European Union countries",
170+
x = "Latest legislative or presidential election (whichever had higher turnout)",
171+
y = "May 2019 European Parliament election",
172+
size = "") +
173+
annotate(geom="text", x=60, y=80, label = paste("European turnout = \n",
174+
round(unname(coef(m)[2]),2),
175+
"x national turnout",
176+
round(unname(coef(m)[1]),1))) +
177+
theme(plot.title = element_text(hjust = 0.5)) +
178+
guides(size=guide_legend(""), fill = FALSE) +
179+
theme(text = element_text(family = 'Fira Sans'))
180+
p <- ggplotly(p)
181+
182+
# Create a shareable link to your chart
183+
# Set up API credentials: https://plot.ly/r/getting-started
184+
chart_link = api_create(p, filename="geom_text/add-formatting")
185+
chart_link
186+
```
187+
188+
<iframe src="https://plot.ly/~RPlotBot/5776.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
189+

0 commit comments

Comments
 (0)