|
| 1 | +--- |
| 2 | +title: geom_text | Examples | Plotly |
| 3 | +name: geom_text |
| 4 | +permalink: ggplot2/geom_text/ |
| 5 | +description: How to make a text graph using ggplotly. |
| 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 | +recent_turnout <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/european_turnout.csv",stringsAsFactors = FALSE) |
| 46 | +recent_turnout$region <- factor(recent_turnout$region, levels=c("British","Northern","Western","Mediterranean","Central/Eastern")) |
| 47 | +
|
| 48 | +library(plotly) |
| 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 | +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")) |
| 75 | +
|
| 76 | +library(plotly) |
| 77 | +p <- recent_turnout %>% |
| 78 | + ggplot(aes(x=nat_turnout,y=euro_turnout)) + |
| 79 | + geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) + |
| 80 | + geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) + |
| 81 | + labs(title = "Recent turnout in European Union countries", |
| 82 | + x = "Latest legislative or presidential election (whichever had higher turnout)", |
| 83 | + y = "May 2019 European Parliament election") |
| 84 | +p <- ggplotly(p) |
| 85 | +
|
| 86 | +# Create a shareable link to your chart |
| 87 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 88 | +chart_link = api_create(p, filename="geom_text/overlaid-points") |
| 89 | +chart_link |
| 90 | +``` |
| 91 | + |
| 92 | +```{r echo=FALSE} |
| 93 | +chart_link |
| 94 | +``` |
| 95 | + |
| 96 | +### Customed Colour and Size Scale |
| 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. |
| 98 | + |
| 99 | +```{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 | +
|
| 103 | +library(plotly) |
| 104 | +library(LaCroixColoR) |
| 105 | +p <- recent_turnout %>% |
| 106 | + ggplot(aes(x=nat_turnout,y=euro_turnout)) + |
| 107 | + geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) + |
| 108 | + geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) + |
| 109 | + scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) + |
| 110 | + scale_size_continuous(range = c(3, 8)) + |
| 111 | + labs(title = "Recent turnout in European Union countries", |
| 112 | + x = "Latest legislative or presidential election (whichever had higher turnout)", |
| 113 | + y = "May 2019 European Parliament election") |
| 114 | +p <- ggplotly(p) |
| 115 | +
|
| 116 | +# Create a shareable link to your chart |
| 117 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 118 | +chart_link = api_create(p, filename="geom_text/customized-scales") |
| 119 | +chart_link |
| 120 | +``` |
| 121 | + |
| 122 | +```{r echo=FALSE} |
| 123 | +chart_link |
| 124 | +``` |
| 125 | + |
| 126 | +### Adding a regression |
| 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). |
| 128 | + |
| 129 | +```{r, results='hide'} |
| 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")) |
| 132 | +m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout) |
| 133 | +
|
| 134 | +library(plotly) |
| 135 | +library(LaCroixColoR) |
| 136 | +p <- recent_turnout %>% |
| 137 | + ggplot(aes(x=nat_turnout,y=euro_turnout)) + |
| 138 | + stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) + |
| 139 | + geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) + |
| 140 | + geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1) + |
| 141 | + scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) + |
| 142 | + scale_size_continuous(range = c(3, 8)) + |
| 143 | + labs(title = "Recent turnout in European Union countries", |
| 144 | + x = "Latest legislative or presidential election (whichever had higher turnout)", |
| 145 | + y = "May 2019 European Parliament election") + |
| 146 | + annotate(geom="text", x=60, y=80, label = paste("European turnout = \n", |
| 147 | + round(unname(coef(m)[2]),2), |
| 148 | + "x national turnout", |
| 149 | + round(unname(coef(m)[1]),1))) |
| 150 | +p <- ggplotly(p) |
| 151 | +
|
| 152 | +# Create a shareable link to your chart |
| 153 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 154 | +chart_link = api_create(p, filename="geom_text/add-regression") |
| 155 | +chart_link |
| 156 | +``` |
| 157 | + |
| 158 | +```{r echo=FALSE} |
| 159 | +chart_link |
| 160 | +``` |
| 161 | + |
| 162 | +### Customized Formatting |
| 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. |
| 164 | + |
| 165 | +```{r, results='hide'} |
| 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")) |
| 168 | +m <- lm(euro_turnout ~ nat_turnout, data = recent_turnout) |
| 169 | +
|
| 170 | +library(plotly) |
| 171 | +library(LaCroixColoR) |
| 172 | +p <- recent_turnout %>% |
| 173 | + ggplot(aes(x=nat_turnout,y=euro_turnout)) + |
| 174 | + stat_smooth(geom="line", method="lm", alpha=0.3, se=FALSE) + |
| 175 | + geom_point(aes(size=population, colour=region, text=paste("country:", country)), alpha=0.4) + |
| 176 | + geom_text(aes(size=population/3.5, label=abbreviation), colour="gray20", alpha=1, family="Fira Sans") + |
| 177 | + scale_colour_manual(values=lacroix_palette(n=6, name="PeachPear")) + |
| 178 | + scale_size_continuous(range = c(3, 8)) + |
| 179 | + labs(title = "Recent turnout in European Union countries", |
| 180 | + x = "Latest legislative or presidential election (whichever had higher turnout)", |
| 181 | + y = "May 2019 European Parliament election", |
| 182 | + size = "") + |
| 183 | + annotate(geom="text", x=60, y=80, label = paste("European turnout = \n", |
| 184 | + round(unname(coef(m)[2]),2), |
| 185 | + "x national turnout", |
| 186 | + round(unname(coef(m)[1]),1))) + |
| 187 | + theme(plot.title = element_text(hjust = 0.5)) + |
| 188 | + guides(size=guide_legend(""), fill = FALSE) + |
| 189 | + theme(text = element_text(family = 'Fira Sans')) |
| 190 | +p <- ggplotly(p) |
| 191 | +
|
| 192 | +# Create a shareable link to your chart |
| 193 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 194 | +chart_link = api_create(p, filename="geom_text/add-formatting") |
| 195 | +chart_link |
| 196 | +``` |
| 197 | + |
| 198 | +```{r echo=FALSE} |
| 199 | +chart_link |
| 200 | +``` |
| 201 | + |
0 commit comments