Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec127d2

Browse files
committed
update with slashes and dataframe assignment
1 parent d83fbe3 commit ec127d2

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

_posts/ggplot2/2019-07-30-geom_text.Rmd

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ packageVersion('plotly')
4242
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).
4343

4444
```{r, results='hide'}
45-
library(plotly)
4645
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
4746
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
4847
48+
library(plotly)
4949
p <- recent_turnout %>%
5050
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
5151
geom_text(aes(size=population/3.5, label=abbreviation, colour=region), alpha=1) +
@@ -65,13 +65,15 @@ chart_link
6565
```
6666

6767
### 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.
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.
6969

7070
Adding the *text* option within aes() allows us to control the text that appears when hovering over a point.
7171

7272
```{r, results='hide'}
73-
library(plotly)
73+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
74+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
7475
76+
library(plotly)
7577
p <- recent_turnout %>%
7678
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
7779
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
@@ -92,12 +94,14 @@ chart_link
9294
```
9395

9496
### 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.
97+
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.
9698

9799
```{r, results='hide'}
100+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
101+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
102+
98103
library(plotly)
99104
library(LaCroixColoR)
100-
101105
p <- recent_turnout %>%
102106
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
103107
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
@@ -120,13 +124,15 @@ chart_link
120124
```
121125

122126
### 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).
127+
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).
124128

125129
```{r, results='hide'}
126-
library(plotly)
127-
library(LaCroixColoR)
130+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
131+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
128132
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
129133
134+
library(plotly)
135+
library(LaCroixColoR)
130136
p <- recent_turnout %>%
131137
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
132138
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
@@ -154,13 +160,15 @@ chart_link
154160
```
155161

156162
### 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.
163+
Changed the font of the geom\_text and of the graph (these must be done separately!), corrected the size label, centre-aligned the title.
158164

159165
```{r, results='hide'}
160-
library(plotly)
161-
library(LaCroixColoR)
166+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
167+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
162168
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
163169
170+
library(plotly)
171+
library(LaCroixColoR)
164172
p <- recent_turnout %>%
165173
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
166174
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +

_posts/ggplot2/2019-07-30-geom_text.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ Sources: [International IDEA](https://www.idea.int/data-tools/continent-view/Eur
4444

4545

4646
```r
47-
library(plotly)
4847
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
4948
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
5049

50+
library(plotly)
5151
p <- recent_turnout %>%
5252
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
5353
geom_text(aes(size=population/3.5, label=abbreviation, colour=region), alpha=1) +
@@ -71,8 +71,10 @@ Adding the *text* option within aes() allows us to control the text that appears
7171

7272

7373
```r
74-
library(plotly)
74+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
75+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
7576

77+
library(plotly)
7678
p <- recent_turnout %>%
7779
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
7880
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
@@ -95,9 +97,11 @@ Let's use the LaCroixColoR package to spruce up the colour scheme. In addition,
9597

9698

9799
```r
100+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
101+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
102+
98103
library(plotly)
99104
library(LaCroixColoR)
100-
101105
p <- recent_turnout %>%
102106
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
103107
geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) +
@@ -122,10 +126,12 @@ Adding a regression line as well as a label. geom_smooth does not allow for adju
122126

123127

124128
```r
125-
library(plotly)
126-
library(LaCroixColoR)
129+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
130+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
127131
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
128132

133+
library(plotly)
134+
library(LaCroixColoR)
129135
p <- recent_turnout %>%
130136
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
131137
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +
@@ -155,10 +161,12 @@ Changed the font of the geom_text and of the graph (these must be done separatel
155161

156162

157163
```r
158-
library(plotly)
159-
library(LaCroixColoR)
164+
recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE)
165+
recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern"))
160166
m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout)
161167

168+
library(plotly)
169+
library(LaCroixColoR)
162170
p <- recent_turnout %>%
163171
ggplot(aes(x=nat_turnout,y=euro_turnout)) +
164172
stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) +

0 commit comments

Comments
 (0)