Skip to content

New ggplot2 docs #94

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 17 commits into from
Sep 28, 2021
Merged
107 changes: 0 additions & 107 deletions ggplot2/2021-08-04-violin.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -200,118 +200,11 @@ p <- p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
plotly::ggplotly(p)
```


# Example


```{r}
library(plotly)
library(ggplot2)

p <-
if (require("ggplot2movies")) {
# Scale transformations occur before the density statistics are computed.
# Coordinate transformations occur afterwards. Observe the effect on the
# number of outliers.
m <- ggplot(movies, aes(y = votes, x = rating, group = cut_width(rating, 0.5)))
m + geom_violin()
m +
geom_violin() +
scale_y_log10()
m +
geom_violin() +
coord_trans(y = "log10")
m +
geom_violin() +
scale_y_log10() + coord_trans(y = "log10")

# Violin plots with continuous x:
# Use the group aesthetic to group observations in violins
ggplot(movies, aes(year, budget)) +
geom_violin()
ggplot(movies, aes(year, budget)) +
geom_violin(aes(group = cut_width(year, 10)), scale = "width")
}

plotly::ggplotly(p)
```

### 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}
library(plotly)
library(ggplot2)

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


### Add facetting
Including facetting by region.

```{r}
library(plotly)
library(ggplot2)

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

### 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}
library(plotly)
library(ggplot2)

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

### 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}
Expand Down