Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93f57b9

Browse files
committed
most of geom_violin, there will be one final push Monday
1 parent c283a5d commit 93f57b9

15 files changed

+370
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ vendor/bundle
2222
_posts/python/fundamentals/static-image/images
2323
_config_personal.yml
2424
_posts/python/html
25+
.Rproj.user
26+
documentation.Rproj

_posts/ggplot2/2016-11-29-geom_boxplot.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 1
12+
order: 2
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-geom_boxplot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 1
12+
order: 2
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-geom_ribbon.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 4
12+
order: 5
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-geom_ribbon.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 4
12+
order: 5
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-geom_smooth.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 5
12+
order: 6
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-geom_smooth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 5
12+
order: 6
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-stat_smooth.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 5
12+
order: 7
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2016-11-29-stat_smooth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 5
12+
order: 7
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2017-04-21-geom_quantile.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 6
12+
order: 4
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2017-04-21-geom_quantile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 6
12+
order: 4
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2019-07-12-geom_bin2d.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 2
12+
order: 1
1313
output:
1414
html_document:
1515
keep_md: true

_posts/ggplot2/2019-07-12-geom_bin2d.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ language: ggplot2
99
page_type: example_index
1010
has_thumbnail: true
1111
display_as: statistical
12-
order: 2
12+
order: 1
1313
output:
1414
html_document:
1515
keep_md: true
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
title: geom_violin | Examples | Plotly
3+
name: geom_violin
4+
permalink: ggplot2/geom_violin/
5+
description: How to make a density map using geom_violin. Includes explanations on flipping axes and facetting.
6+
layout: base
7+
thumbnail: thumbnail/geom_violin.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: statistical
12+
order: 8
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 violin plot
42+
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.
43+
44+
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.
45+
46+
```{r, results='hide'}
47+
library(plotly)
48+
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE)
49+
50+
# MAKE THIS A FACTOR, AND ALSO ADD A THUMBNAIL
51+
52+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
53+
geom_violin(colour=NA) +
54+
geom_hline(yintercept=0, alpha=0.5) +
55+
labs(title = "Democratic performance in the 2018 House elections, by region and density",
56+
x = "Density Index\nfrom CityLab",
57+
y = "Margin of Victory/Defeat")
58+
ggplotly(p)
59+
60+
# Create a shareable link to your chart
61+
# Set up API credentials: https://plot.ly/r/getting-started
62+
chart_link = api_create(p, filename="geom_violin/basic-graph")
63+
chart_link
64+
```
65+
66+
```{r echo=FALSE}
67+
chart_link
68+
```
69+
70+
### Flipping the Axes
71+
With geom_violin(), the y-axis must always be the continuous variable, and the x-axis the ctegorical variable. To create horizontal violin graphs, add coord_flip() without changing .
72+
73+
```{r, results='hide'}
74+
library(plotly)
75+
76+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
77+
geom_violin(colour=NA) +
78+
geom_hline(yintercept=0, alpha=0.5) +
79+
labs(title = "Democratic performance in the 2018 House elections, by region and density",
80+
x = "Density Index\nfrom CityLab",
81+
y = "Margin of Victory/Defeat") +
82+
coord_flip()
83+
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_violin/flip-axes")
88+
chart_link
89+
```
90+
91+
```{r echo=FALSE}
92+
chart_link
93+
```
94+
95+
### Add facetting
96+
Including facetting by region.
97+
98+
```{r, results='hide'}
99+
library(plotly)
100+
101+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
102+
geom_violin(colour=NA) +
103+
geom_hline(yintercept=0, alpha=0.5) +
104+
facet_wrap(~region) +
105+
labs(title = "Democratic performance in the 2018 House elections, by region and density",
106+
x = "Density Index\nfrom CityLab",
107+
y = "Margin of Victory/Defeat") +
108+
coord_flip()
109+
ggplotly(p)
110+
111+
# Create a shareable link to your chart
112+
# Set up API credentials: https://plot.ly/r/getting-started
113+
chart_link = api_create(p, filename="geom_violin/add-facet")
114+
chart_link
115+
```
116+
117+
```{r echo=FALSE}
118+
chart_link
119+
```
120+
121+
### Customized Appearance
122+
Add colour to the facet titles, centre-align the itle, 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.
123+
124+
```{r, results='hide'}
125+
library(plotly)
126+
127+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
128+
geom_violin(colour=NA) +
129+
geom_hline(yintercept=0, alpha=0.5) +
130+
facet_wrap(~region) +
131+
labs(title = "Democratic performance in the 2018 House elections, by region and density",
132+
x = "Density Index\nfrom CityLab",
133+
y = "Margin of Victory/Defeat") +
134+
coord_flip() +
135+
theme(axis.title.y = element_text(angle = 0, vjust=0.5),
136+
plot.title = element_text(hjust = 0.5),
137+
strip.background = element_rect(fill="lightblue"),
138+
text = element_text(family = 'Fira Sans'),
139+
legend.position = "none")
140+
ggplotly(p)
141+
142+
# Create a shareable link to your chart
143+
# Set up API credentials: https://plot.ly/r/getting-started
144+
chart_link = api_create(p, filename="geom_violin/customize-theme")
145+
chart_link
146+
```
147+
148+
```{r echo=FALSE}
149+
chart_link
150+
```
151+
152+
### Rotated Axis Text
153+
Rotated the x-axis text 45 degrees, and used facet_grid to create a 4x4 facet (compared to facet_wrap, which defaults to 2x2).
154+
155+
```{r, results='hide'}
156+
library(plotly)
157+
158+
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
159+
geom_violin(colour=NA) +
160+
geom_hline(yintercept=0, alpha=0.5) +
161+
facet_grid(.~region) +
162+
labs(title = "Democratic performance in the 2018 House elections, by region and density",
163+
x = "Density Index\nfrom CityLab",
164+
y = "Margin of Victory/Defeat") +
165+
theme(axis.text.x = element_text(angle = -45),
166+
plot.title = element_text(hjust = 0.5),
167+
strip.background = element_rect(fill="lightblue"),
168+
text = element_text(family = 'Fira Sans'),
169+
legend.position = "none")
170+
ggplotly(p)
171+
172+
# Create a shareable link to your chart
173+
# Set up API credentials: https://plot.ly/r/getting-started
174+
chart_link = api_create(p, filename="geom_violin/rotated-text")
175+
chart_link
176+
```
177+
178+
```{r echo=FALSE}
179+
chart_link
180+
```
181+

0 commit comments

Comments
 (0)