From 93f57b97df8de55a5082e66049a1026c7ca00d84 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Fri, 2 Aug 2019 18:57:32 -0400 Subject: [PATCH 1/4] most of geom_violin, there will be one final push Monday --- .gitignore | 2 + _posts/ggplot2/2016-11-29-geom_boxplot.Rmd | 2 +- _posts/ggplot2/2016-11-29-geom_boxplot.md | 2 +- _posts/ggplot2/2016-11-29-geom_ribbon.Rmd | 2 +- _posts/ggplot2/2016-11-29-geom_ribbon.md | 2 +- _posts/ggplot2/2016-11-29-geom_smooth.Rmd | 2 +- _posts/ggplot2/2016-11-29-geom_smooth.md | 2 +- _posts/ggplot2/2016-11-29-stat_smooth.Rmd | 2 +- _posts/ggplot2/2016-11-29-stat_smooth.md | 2 +- _posts/ggplot2/2017-04-21-geom_quantile.Rmd | 2 +- _posts/ggplot2/2017-04-21-geom_quantile.md | 2 +- _posts/ggplot2/2019-07-12-geom_bin2d.Rmd | 2 +- _posts/ggplot2/2019-07-12-geom_bin2d.md | 2 +- _posts/ggplot2/2019-08-02-geom_violin.Rmd | 181 ++++++++++++++++++++ _posts/ggplot2/2019-08-02-geom_violin.md | 175 +++++++++++++++++++ 15 files changed, 370 insertions(+), 12 deletions(-) create mode 100644 _posts/ggplot2/2019-08-02-geom_violin.Rmd create mode 100644 _posts/ggplot2/2019-08-02-geom_violin.md diff --git a/.gitignore b/.gitignore index 1172d4bb959c..17603800dfdc 100755 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ vendor/bundle _posts/python/fundamentals/static-image/images _config_personal.yml _posts/python/html +.Rproj.user +documentation.Rproj \ No newline at end of file diff --git a/_posts/ggplot2/2016-11-29-geom_boxplot.Rmd b/_posts/ggplot2/2016-11-29-geom_boxplot.Rmd index 865cfbbcf139..16f3a1646637 100644 --- a/_posts/ggplot2/2016-11-29-geom_boxplot.Rmd +++ b/_posts/ggplot2/2016-11-29-geom_boxplot.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 1 +order: 2 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-geom_boxplot.md b/_posts/ggplot2/2016-11-29-geom_boxplot.md index 3af64fb32685..47e477e78b87 100644 --- a/_posts/ggplot2/2016-11-29-geom_boxplot.md +++ b/_posts/ggplot2/2016-11-29-geom_boxplot.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 1 +order: 2 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-geom_ribbon.Rmd b/_posts/ggplot2/2016-11-29-geom_ribbon.Rmd index 95b526261801..cb1f4d3ea49d 100644 --- a/_posts/ggplot2/2016-11-29-geom_ribbon.Rmd +++ b/_posts/ggplot2/2016-11-29-geom_ribbon.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 4 +order: 5 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-geom_ribbon.md b/_posts/ggplot2/2016-11-29-geom_ribbon.md index 0ea120eca678..92239904c46d 100644 --- a/_posts/ggplot2/2016-11-29-geom_ribbon.md +++ b/_posts/ggplot2/2016-11-29-geom_ribbon.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 4 +order: 5 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-geom_smooth.Rmd b/_posts/ggplot2/2016-11-29-geom_smooth.Rmd index 4f442a1c8410..6880b8344d69 100644 --- a/_posts/ggplot2/2016-11-29-geom_smooth.Rmd +++ b/_posts/ggplot2/2016-11-29-geom_smooth.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 5 +order: 6 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-geom_smooth.md b/_posts/ggplot2/2016-11-29-geom_smooth.md index 5f48fe8ad737..a560b21fd79e 100644 --- a/_posts/ggplot2/2016-11-29-geom_smooth.md +++ b/_posts/ggplot2/2016-11-29-geom_smooth.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 5 +order: 6 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-stat_smooth.Rmd b/_posts/ggplot2/2016-11-29-stat_smooth.Rmd index 115de156b259..5d831a1abe21 100644 --- a/_posts/ggplot2/2016-11-29-stat_smooth.Rmd +++ b/_posts/ggplot2/2016-11-29-stat_smooth.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 5 +order: 7 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2016-11-29-stat_smooth.md b/_posts/ggplot2/2016-11-29-stat_smooth.md index f1fa64da3ba9..cfca776c432d 100644 --- a/_posts/ggplot2/2016-11-29-stat_smooth.md +++ b/_posts/ggplot2/2016-11-29-stat_smooth.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 5 +order: 7 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2017-04-21-geom_quantile.Rmd b/_posts/ggplot2/2017-04-21-geom_quantile.Rmd index 0d8d1a37815b..cf26ec2ea2f0 100644 --- a/_posts/ggplot2/2017-04-21-geom_quantile.Rmd +++ b/_posts/ggplot2/2017-04-21-geom_quantile.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 6 +order: 4 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2017-04-21-geom_quantile.md b/_posts/ggplot2/2017-04-21-geom_quantile.md index 6919c3873495..42d04a9206e1 100644 --- a/_posts/ggplot2/2017-04-21-geom_quantile.md +++ b/_posts/ggplot2/2017-04-21-geom_quantile.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 6 +order: 4 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd b/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd index 3f5c617203c6..4d7206569c9d 100644 --- a/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd +++ b/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 2 +order: 1 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2019-07-12-geom_bin2d.md b/_posts/ggplot2/2019-07-12-geom_bin2d.md index 01f3e602a366..2b69e044fe28 100644 --- a/_posts/ggplot2/2019-07-12-geom_bin2d.md +++ b/_posts/ggplot2/2019-07-12-geom_bin2d.md @@ -9,7 +9,7 @@ language: ggplot2 page_type: example_index has_thumbnail: true display_as: statistical -order: 2 +order: 1 output: html_document: keep_md: true diff --git a/_posts/ggplot2/2019-08-02-geom_violin.Rmd b/_posts/ggplot2/2019-08-02-geom_violin.Rmd new file mode 100644 index 000000000000..32951af3c0a6 --- /dev/null +++ b/_posts/ggplot2/2019-08-02-geom_violin.Rmd @@ -0,0 +1,181 @@ +--- +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!
+[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
+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.
+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)!
+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) + +# MAKE THIS A FACTOR, AND ALSO ADD A THUMBNAIL + +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 ctegorical variable. To create horizontal violin graphs, add coord_flip() without changing . + +```{r, results='hide'} +library(plotly) + +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) + +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 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. + +```{r, results='hide'} +library(plotly) + +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 4x4 facet (compared to facet_wrap, which defaults to 2x2). + +```{r, results='hide'} +library(plotly) + +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 +``` + diff --git a/_posts/ggplot2/2019-08-02-geom_violin.md b/_posts/ggplot2/2019-08-02-geom_violin.md new file mode 100644 index 000000000000..0142488aaf67 --- /dev/null +++ b/_posts/ggplot2/2019-08-02-geom_violin.md @@ -0,0 +1,175 @@ +--- +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 +--- + + + +### New to Plotly? + +Plotly's R library is free and open source!
+[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
+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.
+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)!
+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') +``` + +``` +## [1] '4.9.0.9000' +``` + +### 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) +district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv", stringsAsFactors = FALSE) + +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 +``` + + + +### Flipping the Axes +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 . + + +```r +library(plotly) + +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 +``` + + + +### Add facetting +Including facetting by region. + + +```r +library(plotly) + +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 +``` + + + +### Customized Appearance +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. + + +```r +library(plotly) + +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 +``` + + + +### Rotated Axis Text +Rotated the x-axis text 45 degrees, and used facet_grid to create a 4x4 facet (compared to facet_wrap, which defaults to 2x2). + + +```r +library(plotly) + +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 +``` + + + From e83b9b8a0c5986ae2e1c66ad896c91a48bf47757 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 5 Aug 2019 14:09:53 -0400 Subject: [PATCH 2/4] should be final commit! --- _posts/ggplot2/2019-08-02-geom_violin.Rmd | 8 ++++---- _posts/ggplot2/2019-08-02-geom_violin.md | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/_posts/ggplot2/2019-08-02-geom_violin.Rmd b/_posts/ggplot2/2019-08-02-geom_violin.Rmd index 32951af3c0a6..05e36b60e334 100644 --- a/_posts/ggplot2/2019-08-02-geom_violin.Rmd +++ b/_posts/ggplot2/2019-08-02-geom_violin.Rmd @@ -46,7 +46,7 @@ Source: [Dave Wassermann and Ally Flinn](https://docs.google.com/spreadsheets/d/ ```{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")) # MAKE THIS A FACTOR, AND ALSO ADD A THUMBNAIL p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + @@ -68,7 +68,7 @@ chart_link ``` ### Flipping the Axes -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 . +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) @@ -119,7 +119,7 @@ chart_link ``` ### Customized Appearance -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. +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) @@ -150,7 +150,7 @@ chart_link ``` ### Rotated Axis Text -Rotated the x-axis text 45 degrees, and used facet_grid to create a 4x4 facet (compared to facet_wrap, which defaults to 2x2). +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) diff --git a/_posts/ggplot2/2019-08-02-geom_violin.md b/_posts/ggplot2/2019-08-02-geom_violin.md index 0142488aaf67..88f043cf4564 100644 --- a/_posts/ggplot2/2019-08-02-geom_violin.md +++ b/_posts/ggplot2/2019-08-02-geom_violin.md @@ -48,6 +48,8 @@ Source: [Dave Wassermann and Ally Flinn](https://docs.google.com/spreadsheets/d/ ```r 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")) +# MAKE THIS A FACTOR, AND ALSO ADD A THUMBNAIL p <- ggplot(district_density,aes(x=cluster, y=dem_margin, fill=cluster)) + geom_violin(colour=NA) + @@ -66,7 +68,7 @@ chart_link ### Flipping the Axes -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 . +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 @@ -115,7 +117,7 @@ chart_link ### Customized Appearance -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. +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 @@ -145,7 +147,7 @@ chart_link ### Rotated Axis Text -Rotated the x-axis text 45 degrees, and used facet_grid to create a 4x4 facet (compared to facet_wrap, which defaults to 2x2). +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 From f34d4e3bf4512a8fe46347831bb52835392fe683 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Tue, 6 Aug 2019 17:03:31 -0400 Subject: [PATCH 3/4] FIX --- _posts/ggplot2/2019-08-02-geom_violin.Rmd | 2 +- _posts/ggplot2/2019-08-02-geom_violin.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_posts/ggplot2/2019-08-02-geom_violin.Rmd b/_posts/ggplot2/2019-08-02-geom_violin.Rmd index 05e36b60e334..0dad8a903838 100644 --- a/_posts/ggplot2/2019-08-02-geom_violin.Rmd +++ b/_posts/ggplot2/2019-08-02-geom_violin.Rmd @@ -47,7 +47,7 @@ Source: [Dave Wassermann and Ally Flinn](https://docs.google.com/spreadsheets/d/ 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")) -# MAKE THIS A FACTOR, AND ALSO ADD A THUMBNAIL +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) + diff --git a/_posts/ggplot2/2019-08-02-geom_violin.md b/_posts/ggplot2/2019-08-02-geom_violin.md index 88f043cf4564..3d452fc43cd8 100644 --- a/_posts/ggplot2/2019-08-02-geom_violin.md +++ b/_posts/ggplot2/2019-08-02-geom_violin.md @@ -49,7 +49,7 @@ Source: [Dave Wassermann and Ally Flinn](https://docs.google.com/spreadsheets/d/ 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")) -# MAKE THIS A FACTOR, AND ALSO ADD A THUMBNAIL +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) + From 8df16c24e7dd2021053ec7f39bffac5c07b5fcab Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 12 Aug 2019 17:53:45 -0400 Subject: [PATCH 4/4] assignment --- _posts/ggplot2/2019-08-02-geom_violin.Rmd | 12 ++++++++++++ _posts/ggplot2/2019-08-02-geom_violin.md | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/_posts/ggplot2/2019-08-02-geom_violin.Rmd b/_posts/ggplot2/2019-08-02-geom_violin.Rmd index 0dad8a903838..4337560ca658 100644 --- a/_posts/ggplot2/2019-08-02-geom_violin.Rmd +++ b/_posts/ggplot2/2019-08-02-geom_violin.Rmd @@ -72,6 +72,9 @@ With geom\_violin(), the y-axis must always be the continuous variable, and the ```{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) + @@ -97,6 +100,9 @@ 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) + @@ -123,6 +129,9 @@ Add colour to the facet titles, centre-align the title, rotate the y-axis title, ```{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) + @@ -154,6 +163,9 @@ Rotated the x-axis text 45 degrees, and used facet\_grid to create a 4x1 facet ( ```{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) + diff --git a/_posts/ggplot2/2019-08-02-geom_violin.md b/_posts/ggplot2/2019-08-02-geom_violin.md index 3d452fc43cd8..18b3599474b1 100644 --- a/_posts/ggplot2/2019-08-02-geom_violin.md +++ b/_posts/ggplot2/2019-08-02-geom_violin.md @@ -73,6 +73,9 @@ With geom\_violin(), the y-axis must always be the continuous variable, and the ```r 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) + @@ -97,6 +100,9 @@ Including facetting by region. ```r 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) + @@ -122,6 +128,9 @@ Add colour to the facet titles, centre-align the title, rotate the y-axis title, ```r 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) + @@ -152,6 +161,9 @@ Rotated the x-axis text 45 degrees, and used facet\_grid to create a 4x1 facet ( ```r 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) +