-
-
Notifications
You must be signed in to change notification settings - Fork 544
Geom violin #1439
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
Geom violin #1439
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
93f57b9
most of geom_violin, there will be one final push Monday
kojisposts e83b9b8
should be final commit!
kojisposts f34d4e3
FIX
kojisposts 8df16c2
assignment
kojisposts aa653a2
Merge branch 'source-design-merge' into geom_violin
kojisposts 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
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,193 @@ | ||
--- | ||
title: geom_violin | Examples | Plotly | ||
name: geom_violin | ||
permalink: ggplot2/geom_violin/ | ||
description: How to make a density map using geom_violin. Includes explanations on flipping axes and facetting. | ||
layout: base | ||
thumbnail: thumbnail/geom_violin.jpg | ||
language: ggplot2 | ||
page_type: example_index | ||
has_thumbnail: true | ||
display_as: statistical | ||
order: 8 | ||
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 violin plot | ||
A basic violin plot showing how Democratic vote share in the 2018 elections to the US House of Representatives varied by level of density. A horizontal bar is added, to divide candidates who lost from those who won. | ||
|
||
Source: [Dave Wassermann and Ally Flinn](https://docs.google.com/spreadsheets/d/1WxDaxD5az6kdOjJncmGph37z0BPNhV1fNAH_g7IkpC0/htmlview?sle=true#gid=0) for the election results and CityLab for its [Congressional Density Index](https://github.com/theatlantic/citylab-data/tree/master/citylab-congress). Regional classifications are according to the Census Bureau. | ||
|
||
```{r, results='hide'} | ||
library(plotly) | ||
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) | ||
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) | ||
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) | ||
|
||
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + | ||
geom_violin(colour=NA) + | ||
geom_hline(yintercept=0, alpha=0.5) + | ||
labs(title = "Democratic performance in the 2018 House elections, by region and density", | ||
x = "Density Index\nfrom CityLab", | ||
y = "Margin of Victory/Defeat") | ||
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_violin/basic-graph") | ||
chart_link | ||
``` | ||
|
||
```{r echo=FALSE} | ||
chart_link | ||
``` | ||
|
||
### Flipping the Axes | ||
With geom\_violin(), the y-axis must always be the continuous variable, and the x-axis the categorical variable. To create horizontal violin graphs, keep the x- and y-variables as is and add coord\_flip(). | ||
|
||
```{r, results='hide'} | ||
library(plotly) | ||
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) | ||
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) | ||
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) | ||
|
||
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + | ||
geom_violin(colour=NA) + | ||
geom_hline(yintercept=0, alpha=0.5) + | ||
labs(title = "Democratic performance in the 2018 House elections, by region and density", | ||
x = "Density Index\nfrom CityLab", | ||
y = "Margin of Victory/Defeat") + | ||
coord_flip() | ||
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_violin/flip-axes") | ||
chart_link | ||
``` | ||
|
||
```{r echo=FALSE} | ||
chart_link | ||
``` | ||
|
||
### Add facetting | ||
Including facetting by region. | ||
|
||
```{r, results='hide'} | ||
library(plotly) | ||
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) | ||
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) | ||
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) | ||
|
||
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + | ||
geom_violin(colour=NA) + | ||
geom_hline(yintercept=0, alpha=0.5) + | ||
facet_wrap(~region) + | ||
labs(title = "Democratic performance in the 2018 House elections, by region and density", | ||
x = "Density Index\nfrom CityLab", | ||
y = "Margin of Victory/Defeat") + | ||
coord_flip() | ||
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_violin/add-facet") | ||
chart_link | ||
``` | ||
|
||
```{r echo=FALSE} | ||
chart_link | ||
``` | ||
|
||
### Customized Appearance | ||
Add colour to the facet titles, centre-align the title, rotate the y-axis title, change the font, and get rid of the unnecessary legend. Note that coord_flip() flips the axes for the variables and the titles, but does not flip theme() elements. | ||
|
||
```{r, results='hide'} | ||
library(plotly) | ||
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) | ||
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) | ||
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) | ||
|
||
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + | ||
geom_violin(colour=NA) + | ||
geom_hline(yintercept=0, alpha=0.5) + | ||
facet_wrap(~region) + | ||
labs(title = "Democratic performance in the 2018 House elections, by region and density", | ||
x = "Density Index\nfrom CityLab", | ||
y = "Margin of Victory/Defeat") + | ||
coord_flip() + | ||
theme(axis.title.y = element_text(angle = 0, vjust=0.5), | ||
plot.title = element_text(hjust = 0.5), | ||
strip.background = element_rect(fill="lightblue"), | ||
text = element_text(family = 'Fira Sans'), | ||
legend.position = "none") | ||
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_violin/customize-theme") | ||
chart_link | ||
``` | ||
|
||
```{r echo=FALSE} | ||
chart_link | ||
``` | ||
|
||
### Rotated Axis Text | ||
Rotated the x-axis text 45 degrees, and used facet\_grid to create a 4x1 facet (compared to facet\_wrap, which defaults to 2x2). | ||
|
||
```{r, results='hide'} | ||
library(plotly) | ||
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) | ||
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) | ||
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) | ||
|
||
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + | ||
geom_violin(colour=NA) + | ||
geom_hline(yintercept=0, alpha=0.5) + | ||
facet_grid(.~region) + | ||
labs(title = "Democratic performance in the 2018 House elections, by region and density", | ||
x = "Density Index\nfrom CityLab", | ||
y = "Margin of Victory/Defeat") + | ||
theme(axis.text.x = element_text(angle = -45), | ||
plot.title = element_text(hjust = 0.5), | ||
strip.background = element_rect(fill="lightblue"), | ||
text = element_text(family = 'Fira Sans'), | ||
legend.position = "none") | ||
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_violin/rotated-text") | ||
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.
👍 for escaping the underscore