Skip to content

Commit 9be20f1

Browse files
committed
Update 2021-08-04-violin.Rmd
1 parent e26221a commit 9be20f1

File tree

1 file changed

+1
-107
lines changed

1 file changed

+1
-107
lines changed

ggplot2/2021-08-04-violin.Rmd

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ library(ggplot2)
5151
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE)
5252
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural"))
5353
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast"))
54-
54+
3
5555
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
5656
geom_violin(colour=NA) +
5757
geom_hline(yintercept=0, alpha=0.5) +
@@ -200,118 +200,12 @@ p <- p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
200200
plotly::ggplotly(p)
201201
```
202202

203-
204-
# Example
205-
206-
207-
```{r}
208-
library(plotly)
209-
library(ggplot2)
210-
211-
p <-
212-
if (require("ggplot2movies")) {
213-
# Scale transformations occur before the density statistics are computed.
214-
# Coordinate transformations occur afterwards. Observe the effect on the
215-
# number of outliers.
216-
m <- ggplot(movies, aes(y = votes, x = rating, group = cut_width(rating, 0.5)))
217-
m + geom_violin()
218-
m +
219-
geom_violin() +
220-
scale_y_log10()
221-
m +
222-
geom_violin() +
223-
coord_trans(y = "log10")
224-
m +
225-
geom_violin() +
226-
scale_y_log10() + coord_trans(y = "log10")
227-
228-
# Violin plots with continuous x:
229-
# Use the group aesthetic to group observations in violins
230-
ggplot(movies, aes(year, budget)) +
231-
geom_violin()
232-
ggplot(movies, aes(year, budget)) +
233-
geom_violin(aes(group = cut_width(year, 10)), scale = "width")
234-
}
235-
236-
plotly::ggplotly(p)
237-
```
238-
239-
### Basic violin plot
240-
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.
241-
242-
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.
243-
244-
```{r}
245-
library(plotly)
246-
library(ggplot2)
247-
248-
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE)
249-
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural"))
250-
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast"))
251-
252-
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
253-
geom_violin(colour=NA) +
254-
geom_hline(yintercept=0, alpha=0.5) +
255-
labs(title = "Democratic performance in the 2018 House elections, by region and density",
256-
x = "Density Index\nfrom CityLab",
257-
y = "Margin of Victory/Defeat")
258-
259-
ggplotly(p)
260-
```
261-
262-
263203
### Add facetting
264204
Including facetting by region.
265205

266-
```{r}
267-
library(plotly)
268-
library(ggplot2)
269-
270-
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE)
271-
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural"))
272-
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast"))
273-
274-
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
275-
geom_violin(colour=NA) +
276-
geom_hline(yintercept=0, alpha=0.5) +
277-
facet_wrap(~region) +
278-
labs(title = "Democratic performance in the 2018 House elections, by region and density",
279-
x = "Density Index\nfrom CityLab",
280-
y = "Margin of Victory/Defeat") +
281-
coord_flip()
282-
283-
ggplotly(p)
284-
```
285-
286-
### Customized Appearance
287206
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.
288207

289-
```{r}
290-
library(plotly)
291-
library(ggplot2)
292-
293-
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE)
294-
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural"))
295-
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast"))
296-
297-
p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) +
298-
geom_violin(colour=NA) +
299-
geom_hline(yintercept=0, alpha=0.5) +
300-
facet_wrap(~region) +
301-
labs(title = "Democratic performance in the 2018 House elections, by region and density",
302-
x = "Density Index\nfrom CityLab",
303-
y = "Margin of Victory/Defeat") +
304-
coord_flip() +
305-
theme(axis.title.y = element_text(angle = 0, vjust=0.5),
306-
plot.title = element_text(hjust = 0.5),
307-
strip.background = element_rect(fill="lightblue"),
308-
text = element_text(family = 'Fira Sans'),
309-
legend.position = "none")
310-
311-
ggplotly(p)
312-
```
313208

314-
### Rotated Axis Text
315209
Rotated the x-axis text 45 degrees, and used `facet_grid` to create a 4x1 facet (compared to `facet_wrap`, which defaults to 2x2).
316210

317211
```{r}

0 commit comments

Comments
 (0)