diff --git a/.gitignore b/.gitignore index 1172d4bb959c..92d80f1c610d 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 diff --git a/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd b/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd index 3f5c617203c6..f1eaf9c282f9 100644 --- a/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd +++ b/_posts/ggplot2/2019-07-12-geom_bin2d.Rmd @@ -39,6 +39,9 @@ packageVersion('plotly') ``` ### Basic 2d Heatmap +See also geom\_hex for a similar geom with hexagonal bins. Note: facetting is supported in geom\_bin2d but not geom\_hex. + +Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158) ```{r, results='hide'} library(plotly) @@ -100,7 +103,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) + labs(title = "Distribution of the Canadian population by English and French fluency", x = "% fluent in English", y = "% fluent in French", - fill = "# of people") + fill = "population") p <- ggplotly(p) @@ -127,7 +130,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + labs(title = "Distribution of Canadian towns by English and French fluency", x = "% fluent in English", y = "% fluent in French", - fill = "# of people") + fill = "population") p <- ggplotly(p) @@ -154,7 +157,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + labs(title = "Distribution of Canadian towns by English and French fluency", x = "% fluent in English", y = "% fluent in French", - fill = "# of people") + + fill = "population") + theme_bw() + theme(text = element_text(family = 'Fira Sans')) p <- ggplotly(p) diff --git a/_posts/ggplot2/2019-07-12-geom_bin2d.md b/_posts/ggplot2/2019-07-12-geom_bin2d.md index 01f3e602a366..8d9424d07cc2 100644 --- a/_posts/ggplot2/2019-07-12-geom_bin2d.md +++ b/_posts/ggplot2/2019-07-12-geom_bin2d.md @@ -36,11 +36,13 @@ packageVersion('plotly') ``` ``` -## [1] '4.9.0' +## [1] '4.9.0.9000' ``` ### Basic 2d Heatmap +See also [geom_hex](https://plot.ly/ggplot2/geom_hex/) for a similar geom with hexagonal bins. Note: facetting is supported in geom\_bin2d but not geom\_hex. +Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158) ```r library(plotly) @@ -100,7 +102,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) + labs(title = "Distribution of the Canadian population by English and French fluency", x = "% fluent in English", y = "% fluent in French", - fill = "# of people") + fill = "population") p <- ggplotly(p) @@ -113,7 +115,7 @@ chart_link ### With Facets -We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides. +We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides. ```r @@ -126,7 +128,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + labs(title = "Distribution of Canadian towns by English and French fluency", x = "% fluent in English", y = "% fluent in French", - fill = "# of people") + fill = "population") p <- ggplotly(p) @@ -152,7 +154,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + labs(title = "Distribution of Canadian towns by English and French fluency", x = "% fluent in English", y = "% fluent in French", - fill = "# of people") + + fill = "population") + theme_bw() + theme(text = element_text(family = 'Fira Sans')) p <- ggplotly(p) diff --git a/_posts/ggplot2/2019-07-30-geom_hex.Rmd b/_posts/ggplot2/2019-07-30-geom_hex.Rmd new file mode 100644 index 000000000000..3f3fa85bfeab --- /dev/null +++ b/_posts/ggplot2/2019-07-30-geom_hex.Rmd @@ -0,0 +1,153 @@ +--- +title: geom_hex | Examples | Plotly +name: geom_hex +permalink: ggplot2/geom_hex/ +description: How to make a hexagonal two-dimensional heatmap in ggplot2 using geom_hex. Examples of coloured and facetted graphs. +layout: base +thumbnail: thumbnail/geom_hex.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: statistical +order: 3 +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 2d Heatmap +See also [geom_bin2d](https://plot.ly/ggplot2/geom_bin2d/) for a similar geom with rectangular bins. Note: facetting is supported in geom\_bin2d but not geom\_hex. + +Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158) + +```{r, results='hide'} +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc,y=frenperc)) + + geom_hex() + + labs(title = "Distribution of Canadian areas by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "# of census \nsubdivisions") +p <- 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_hex/2d-chart") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Customized Colours +Let's flip the colour scheme so that lighter colours denote larger numbers than darker colours. We should also move to a logarithmic scale, since as it is, the very large value in the bottom right overshadows all other values. + +```{r, results='hide'} +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc,y=frenperc)) + + geom_hex() + + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + + labs(title = "Distribution of Canadian towns by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "# of census \nsubdivisions") +p <- 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_hex/log-chart") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Weighted Data +In the previous graphs, each observation represented a single census subdivision - this counted small towns of 500 people equally with cities like Montreal and Toronto. We can weight the data by the "total" column (i.e. total population) to make this a graph of population. + +```{r, results='hide'} +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) + + geom_hex() + + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + + labs(title = "Distribution of the Canadian population by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "population") +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_hex/weighted-data") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Customized Appearance +We can modify the graph's appearance - for example, if the grey background makes it difficult to make out the paler shades of blue, we can change the theme to one with a white background. Included also is a way to change the font. You can find a list [here](http://ggplot2.tidyverse.org/reference/theme.html) of all the theme elements that you can modify. + +```{r, results='hide'} +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + + geom_hex(bins = 20) + + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + + labs(title = "Distribution of Canadian towns by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "population") + + theme_bw() + + theme(text = element_text(family = 'Fira Sans')) +p <- 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_hex/customize-theme") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + diff --git a/_posts/ggplot2/2019-07-30-geom_hex.md b/_posts/ggplot2/2019-07-30-geom_hex.md new file mode 100644 index 000000000000..d1da46c70d79 --- /dev/null +++ b/_posts/ggplot2/2019-07-30-geom_hex.md @@ -0,0 +1,148 @@ +--- +title: geom_hex | Examples | Plotly +name: geom_hex +permalink: ggplot2/geom_hex/ +description: How to make a hexagonal two-dimensional heatmap in ggplot2 using geom_hex. Examples of coloured and facetted graphs. +layout: base +thumbnail: thumbnail/geom_hex.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: statistical +order: 3 +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 2d Heatmap +See also [geom_bin2d](https://plot.ly/ggplot2/geom_bin2d/) for a similar geom with rectangular bins. Note: facetting is supported in geom\_bin2d but not geom\_hex. + +Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158) + +```r +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc,y=frenperc)) + + geom_hex() + + labs(title = "Distribution of Canadian areas by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "# of census \nsubdivisions") +p <- 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_hex/2d-chart") +chart_link +``` + + + +### Customized Colours +Let's flip the colour scheme so that lighter colours denote larger numbers than darker colours. We should also move to a logarithmic scale, since as it is, the very large value in the bottom right overshadows all other values. + + +```r +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc,y=frenperc)) + + geom_hex() + + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + + labs(title = "Distribution of Canadian towns by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "# of census \nsubdivisions") +p <- 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_hex/log-chart") +chart_link +``` + + + +### Weighted Data +In the previous graphs, each observation represented a single census subdivision - this counted small towns of 500 people equally with cities like Montreal and Toronto. We can weight the data by the "total" column (i.e. total population) to make this a graph of population. + + +```r +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) + + geom_hex() + + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + + labs(title = "Distribution of the Canadian population by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "population") +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_hex/weighted-data") +chart_link +``` + + + +### Customized Appearance +We can modify the graph's appearance - for example, if the grey background makes it difficult to make out the paler shades of blue, we can change the theme to one with a white background. Included also is a way to change the font. You can find a list [here](http://ggplot2.tidyverse.org/reference/theme.html) of all the theme elements that you can modify. + + +```r +library(plotly) + +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) + +p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + + geom_hex(bins = 20) + + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + + labs(title = "Distribution of Canadian towns by English and French fluency", + x = "% fluent in English", + y = "% fluent in French", + fill = "population") + + theme_bw() + + theme(text = element_text(family = 'Fira Sans')) +p <- 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_hex/customize-theme") +chart_link +``` + + +