-
-
Notifications
You must be signed in to change notification settings - Fork 544
added geom_text #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
added geom_text #1435
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
--- | ||
title: geom_text | Examples | Plotly | ||
name: geom_text | ||
permalink: ggplot2/geom_text/ | ||
description: How to make a text graph using ggplotly. | ||
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!<br> | ||
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> | ||
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> | ||
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)!<br> | ||
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'} | ||
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) + | ||
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'} | ||
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) + | ||
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'} | ||
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) + | ||
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'} | ||
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) + | ||
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'} | ||
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) + | ||
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 | ||
``` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add dataframe assignment here so that the example runs without having run the first example first.