|
| 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