From 357761ac8a0c05220a4bed3f479961e62d673447 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Thu, 1 Aug 2019 18:44:17 -0400 Subject: [PATCH 1/3] added geom_text --- _posts/ggplot2/2019-07-30-geom_text.Rmd | 193 ++++++++++++++++++++++++ _posts/ggplot2/2019-07-30-geom_text.md | 189 +++++++++++++++++++++++ 2 files changed, 382 insertions(+) create mode 100644 _posts/ggplot2/2019-07-30-geom_text.Rmd create mode 100644 _posts/ggplot2/2019-07-30-geom_text.md diff --git a/_posts/ggplot2/2019-07-30-geom_text.Rmd b/_posts/ggplot2/2019-07-30-geom_text.Rmd new file mode 100644 index 000000000000..c26b4254e5cb --- /dev/null +++ b/_posts/ggplot2/2019-07-30-geom_text.Rmd @@ -0,0 +1,193 @@ +--- +title: geom_text | Examples | Plotly +name: geom_text +permalink: ggplot2/geom_text/ +description: How to add text . +layout: base +thumbnail: thumbnail/geom_text.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: basic +order: 11 +output: + html_document: + keep_md: true +--- + +```{r, echo = FALSE, message=FALSE} +knitr::opts_chunk$set(message = FALSE, warning=FALSE) +Sys.setenv("plotly_username"="RPlotBot") +Sys.setenv("plotly_api_key"="q0lz6r5efr") +``` + +### New to Plotly? + +Plotly's R library is free and open source!
+[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
+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.
+We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! + +### Version Check + +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!
+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. + +```{r} +library(plotly) +packageVersion('plotly') +``` + +### 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, results='hide'} +library(plotly) +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")) + +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") +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/basic-chart") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### 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, results='hide'} +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") +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/overlaid-points") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### 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, results='hide'} +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") +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/customized-scales") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### 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, results='hide'} +library(plotly) +library(LaCroixColoR) +m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout) + +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))) +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/add-regression") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### 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, results='hide'} +library(plotly) +library(LaCroixColoR) +m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout) + +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')) +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/add-formatting") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + diff --git a/_posts/ggplot2/2019-07-30-geom_text.md b/_posts/ggplot2/2019-07-30-geom_text.md new file mode 100644 index 000000000000..53419e9840c3 --- /dev/null +++ b/_posts/ggplot2/2019-07-30-geom_text.md @@ -0,0 +1,189 @@ +--- +title: geom_text | Examples | Plotly +name: geom_text +permalink: ggplot2/geom_text/ +description: How to add text . +layout: base +thumbnail: thumbnail/geom_text.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: basic +order: 11 +output: + html_document: + keep_md: true +--- + + + +### New to Plotly? + +Plotly's R library is free and open source!
+[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
+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.
+We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! + +### Version Check + +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!
+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. + + +```r +library(plotly) +packageVersion('plotly') +``` + +``` +## [1] '4.9.0.9000' +``` + +### 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 +library(plotly) +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")) + +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") +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/basic-chart") +chart_link +``` + + + +### 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 +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") +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/overlaid-points") +chart_link +``` + + + +### 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 +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") +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/customized-scales") +chart_link +``` + + + +### 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 +library(plotly) +library(LaCroixColoR) +m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout) + +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))) +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/add-regression") +chart_link +``` + + + +### 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 +library(plotly) +library(LaCroixColoR) +m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout) + +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')) +p <- ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_text/add-formatting") +chart_link +``` + + + From d83fbe3e026d4425a0123e590a8fc3c733e486e3 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Thu, 1 Aug 2019 18:59:10 -0400 Subject: [PATCH 2/3] making sure the geoms are alphabetical --- _posts/ggplot2/2016-11-29-geom_tile.Rmd | 2 +- _posts/ggplot2/2016-11-29-geom_tile.md | 2 +- _posts/ggplot2/2017-04-21-geom_spoke.Rmd | 2 +- _posts/ggplot2/2017-04-21-geom_spoke.md | 2 +- _posts/ggplot2/2018-06-22-geom_sf.Rmd | 2 +- _posts/ggplot2/2018-06-22-geom_sf.md | 2 +- _posts/ggplot2/2019-07-30-geom_text.Rmd | 2 +- _posts/ggplot2/2019-07-30-geom_text.md | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/_posts/ggplot2/2016-11-29-geom_tile.Rmd b/_posts/ggplot2/2016-11-29-geom_tile.Rmd index cf82ede17a62..e1bdc450edab 100644 --- a/_posts/ggplot2/2016-11-29-geom_tile.Rmd +++ b/_posts/ggplot2/2016-11-29-geom_tile.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: basic -order: 8 +order: 12 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-geom_tile.md b/_posts/ggplot2/2016-11-29-geom_tile.md index dea0a2ee34e1..56312ce8f7d8 100644 --- a/_posts/ggplot2/2016-11-29-geom_tile.md +++ b/_posts/ggplot2/2016-11-29-geom_tile.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: basic -order: 8 +order: 12 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2017-04-21-geom_spoke.Rmd b/_posts/ggplot2/2017-04-21-geom_spoke.Rmd index 6d7de70a3d11..2e7c3a8563d4 100644 --- a/_posts/ggplot2/2017-04-21-geom_spoke.Rmd +++ b/_posts/ggplot2/2017-04-21-geom_spoke.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: basic -order: 11 +order: 10 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2017-04-21-geom_spoke.md b/_posts/ggplot2/2017-04-21-geom_spoke.md index cc7dc26717b2..08253708d3c9 100644 --- a/_posts/ggplot2/2017-04-21-geom_spoke.md +++ b/_posts/ggplot2/2017-04-21-geom_spoke.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: basic -order: 11 +order: 10 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2018-06-22-geom_sf.Rmd b/_posts/ggplot2/2018-06-22-geom_sf.Rmd index e1ee45a41202..f026bdd3987b 100644 --- a/_posts/ggplot2/2018-06-22-geom_sf.Rmd +++ b/_posts/ggplot2/2018-06-22-geom_sf.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: basic -order: 12 +order: 9 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2018-06-22-geom_sf.md b/_posts/ggplot2/2018-06-22-geom_sf.md index fe3a51575acf..f4467b7219e4 100644 --- a/_posts/ggplot2/2018-06-22-geom_sf.md +++ b/_posts/ggplot2/2018-06-22-geom_sf.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: basic -order: 12 +order: 9 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2019-07-30-geom_text.Rmd b/_posts/ggplot2/2019-07-30-geom_text.Rmd index c26b4254e5cb..1c1325ca71c5 100644 --- a/_posts/ggplot2/2019-07-30-geom_text.Rmd +++ b/_posts/ggplot2/2019-07-30-geom_text.Rmd @@ -2,7 +2,7 @@ title: geom_text | Examples | Plotly name: geom_text permalink: ggplot2/geom_text/ -description: How to add text . +description: How to make a text graph using ggplotly. layout: base thumbnail: thumbnail/geom_text.jpg language: ggplot2 diff --git a/_posts/ggplot2/2019-07-30-geom_text.md b/_posts/ggplot2/2019-07-30-geom_text.md index 53419e9840c3..bb7d3f9c6c68 100644 --- a/_posts/ggplot2/2019-07-30-geom_text.md +++ b/_posts/ggplot2/2019-07-30-geom_text.md @@ -2,7 +2,7 @@ title: geom_text | Examples | Plotly name: geom_text permalink: ggplot2/geom_text/ -description: How to add text . +description: How to make a text graph using ggplotly. layout: base thumbnail: thumbnail/geom_text.jpg language: ggplot2 From ec127d232fd6dcb5b122ba2fd35f871961312e2b Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 12 Aug 2019 17:51:07 -0400 Subject: [PATCH 3/3] update with slashes and dataframe assignment --- _posts/ggplot2/2019-07-30-geom_text.Rmd | 30 ++++++++++++++++--------- _posts/ggplot2/2019-07-30-geom_text.md | 22 ++++++++++++------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/_posts/ggplot2/2019-07-30-geom_text.Rmd b/_posts/ggplot2/2019-07-30-geom_text.Rmd index 1c1325ca71c5..492ef48fd8ac 100644 --- a/_posts/ggplot2/2019-07-30-geom_text.Rmd +++ b/_posts/ggplot2/2019-07-30-geom_text.Rmd @@ -42,10 +42,10 @@ packageVersion('plotly') 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, results='hide'} -library(plotly) 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) + @@ -65,13 +65,15 @@ chart_link ``` ### 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. +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, results='hide'} -library(plotly) +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) + @@ -92,12 +94,14 @@ chart_link ``` ### 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. +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, results='hide'} +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) + @@ -120,13 +124,15 @@ chart_link ``` ### 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). +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, results='hide'} -library(plotly) -library(LaCroixColoR) +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) + @@ -154,13 +160,15 @@ chart_link ``` ### 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. +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, results='hide'} -library(plotly) -library(LaCroixColoR) +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) + diff --git a/_posts/ggplot2/2019-07-30-geom_text.md b/_posts/ggplot2/2019-07-30-geom_text.md index bb7d3f9c6c68..66a24008bc7f 100644 --- a/_posts/ggplot2/2019-07-30-geom_text.md +++ b/_posts/ggplot2/2019-07-30-geom_text.md @@ -44,10 +44,10 @@ Sources: [International IDEA](https://www.idea.int/data-tools/continent-view/Eur ```r -library(plotly) 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) + @@ -71,8 +71,10 @@ Adding the *text* option within aes() allows us to control the text that appears ```r -library(plotly) +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) + @@ -95,9 +97,11 @@ Let's use the LaCroixColoR package to spruce up the colour scheme. In addition, ```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) + @@ -122,10 +126,12 @@ Adding a regression line as well as a label. geom_smooth does not allow for adju ```r -library(plotly) -library(LaCroixColoR) +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) + @@ -155,10 +161,12 @@ Changed the font of the geom_text and of the graph (these must be done separatel ```r -library(plotly) -library(LaCroixColoR) +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) +