|
| 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 | +district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) |
| 50 | +district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) |
| 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 categorical variable. To create horizontal violin graphs, keep the x- and y-variables as is and add coord\_flip(). |
| 72 | + |
| 73 | +```{r, results='hide'} |
| 74 | +library(plotly) |
| 75 | +district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) |
| 76 | +district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) |
| 77 | +district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) |
| 78 | +
|
| 79 | +p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + |
| 80 | + geom_violin(colour=NA) + |
| 81 | + geom_hline(yintercept=0, alpha=0.5) + |
| 82 | + labs(title = "Democratic performance in the 2018 House elections, by region and density", |
| 83 | + x = "Density Index\nfrom CityLab", |
| 84 | + y = "Margin of Victory/Defeat") + |
| 85 | + coord_flip() |
| 86 | +ggplotly(p) |
| 87 | +
|
| 88 | +# Create a shareable link to your chart |
| 89 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 90 | +chart_link = api_create(p, filename="geom_violin/flip-axes") |
| 91 | +chart_link |
| 92 | +``` |
| 93 | + |
| 94 | +```{r echo=FALSE} |
| 95 | +chart_link |
| 96 | +``` |
| 97 | + |
| 98 | +### Add facetting |
| 99 | +Including facetting by region. |
| 100 | + |
| 101 | +```{r, results='hide'} |
| 102 | +library(plotly) |
| 103 | +district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) |
| 104 | +district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) |
| 105 | +district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) |
| 106 | +
|
| 107 | +p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + |
| 108 | + geom_violin(colour=NA) + |
| 109 | + geom_hline(yintercept=0, alpha=0.5) + |
| 110 | + facet_wrap(~region) + |
| 111 | + labs(title = "Democratic performance in the 2018 House elections, by region and density", |
| 112 | + x = "Density Index\nfrom CityLab", |
| 113 | + y = "Margin of Victory/Defeat") + |
| 114 | + coord_flip() |
| 115 | +ggplotly(p) |
| 116 | +
|
| 117 | +# Create a shareable link to your chart |
| 118 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 119 | +chart_link = api_create(p, filename="geom_violin/add-facet") |
| 120 | +chart_link |
| 121 | +``` |
| 122 | + |
| 123 | +```{r echo=FALSE} |
| 124 | +chart_link |
| 125 | +``` |
| 126 | + |
| 127 | +### Customized Appearance |
| 128 | +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. |
| 129 | + |
| 130 | +```{r, results='hide'} |
| 131 | +library(plotly) |
| 132 | +district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) |
| 133 | +district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) |
| 134 | +district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) |
| 135 | +
|
| 136 | +p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + |
| 137 | + geom_violin(colour=NA) + |
| 138 | + geom_hline(yintercept=0, alpha=0.5) + |
| 139 | + facet_wrap(~region) + |
| 140 | + labs(title = "Democratic performance in the 2018 House elections, by region and density", |
| 141 | + x = "Density Index\nfrom CityLab", |
| 142 | + y = "Margin of Victory/Defeat") + |
| 143 | + coord_flip() + |
| 144 | + theme(axis.title.y = element_text(angle = 0, vjust=0.5), |
| 145 | + plot.title = element_text(hjust = 0.5), |
| 146 | + strip.background = element_rect(fill="lightblue"), |
| 147 | + text = element_text(family = 'Fira Sans'), |
| 148 | + legend.position = "none") |
| 149 | +ggplotly(p) |
| 150 | +
|
| 151 | +# Create a shareable link to your chart |
| 152 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 153 | +chart_link = api_create(p, filename="geom_violin/customize-theme") |
| 154 | +chart_link |
| 155 | +``` |
| 156 | + |
| 157 | +```{r echo=FALSE} |
| 158 | +chart_link |
| 159 | +``` |
| 160 | + |
| 161 | +### Rotated Axis Text |
| 162 | +Rotated the x-axis text 45 degrees, and used facet\_grid to create a 4x1 facet (compared to facet\_wrap, which defaults to 2x2). |
| 163 | + |
| 164 | +```{r, results='hide'} |
| 165 | +library(plotly) |
| 166 | +district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) |
| 167 | +district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural")) |
| 168 | +district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast")) |
| 169 | +
|
| 170 | +p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + |
| 171 | + geom_violin(colour=NA) + |
| 172 | + geom_hline(yintercept=0, alpha=0.5) + |
| 173 | + facet_grid(.~region) + |
| 174 | + labs(title = "Democratic performance in the 2018 House elections, by region and density", |
| 175 | + x = "Density Index\nfrom CityLab", |
| 176 | + y = "Margin of Victory/Defeat") + |
| 177 | + theme(axis.text.x = element_text(angle = -45), |
| 178 | + plot.title = element_text(hjust = 0.5), |
| 179 | + strip.background = element_rect(fill="lightblue"), |
| 180 | + text = element_text(family = 'Fira Sans'), |
| 181 | + legend.position = "none") |
| 182 | +ggplotly(p) |
| 183 | +
|
| 184 | +# Create a shareable link to your chart |
| 185 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 186 | +chart_link = api_create(p, filename="geom_violin/rotated-text") |
| 187 | +chart_link |
| 188 | +``` |
| 189 | + |
| 190 | +```{r echo=FALSE} |
| 191 | +chart_link |
| 192 | +``` |
| 193 | + |
0 commit comments