Skip to content

New ggplot2 docs #93

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 15 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
sudo R -e 'devtools::install_github("hypertidy/anglr@v0.7.0", dependencies = TRUE) '
sudo R -e 'devtools::install_github("plotly/dash-daq", dependencies = TRUE, upgrade = TRUE) '
sudo R -e 'devtools::install_deps(dependencies = TRUE) '
sudo R -e 'devtools::install_github("briatte/ggnet", dependencies = TRUE) '
- save_cache:
key: cache4
paths:
Expand Down
15 changes: 14 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ Imports:
nlme,
Lahman,
quantreg,
backports
backports,
mapproj,
ggrepel,
ggdendro,
treemapify,
GGally,
tree,
ggfortify,
cluster,
hrbrthemes,
ggQC,
fmsb,
plotROC,
tidyquant
105 changes: 105 additions & 0 deletions ggplot2/2021-08-04-2D-Histogram.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
name: 2D-Histogram
permalink: ggplot2/geom_density2d/
description: How to make 2D-Histogram Plots plots in ggplot2 with Plotly.
layout: base
thumbnail: thumbnail/histogram2d.jpg
language: ggplot2
page_type: u-guide
display_as: statistical
order: 5
output:
html_document:
keep_md: true
---

```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
### Basic 2D Graph
Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data)

```{r}
library(plotly)
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)

p <- ggplot(beers, aes(x=abv, y=ibu)) +
geom_density2d() +
labs(y = "bitterness (IBU)",
x = "alcohol volume (ABV)",
title = "Craft beers from American breweries")

ggplotly(p)
```

### Filled
Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph.

```{r}
library(plotly)

beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)

p <- ggplot(beers, aes(x=abv, y=ibu)) +
stat_density2d(aes(fill = stat(level)), geom="polygon") +
labs(y = "bitterness (IBU)",
x = "alcohol volume (ABV)",
title = "Craft beers from American breweries")

ggplotly(p)
```

### Preset Colourscale
["Viridis" colourscales](https://ggplot2.tidyverse.org/reference/scale_viridis.html) are designed to still be perceptible in black-and-white, as well as for those with colourblindness. It comes with five colourscales, selected using the option= parameter: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default), and "cividis" (or "E").

```{r}
library(plotly)
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)

p <- ggplot(beers, aes(x=abv, y=ibu)) +
stat_density2d(aes(fill = stat(level)), geom="polygon") +
scale_fill_viridis_c(option = "plasma") +
theme(legend.position = "magma") +
labs(y = "bitterness (IBU)",
x = "alcohol volume (ABV)",
title = "Craft beers from American breweries")

ggplotly(p)
```

### Customized Colourscale
You can also set your own colour gradients by defining a high and low point.
```{r}
library(plotly)
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)

p <- ggplot(beers, aes(x=abv, y=ibu)) +
stat_density2d(aes(fill = stat(level)), geom="polygon") +
scale_fill_gradient(low = "lightskyblue1", high = "darkred") +
theme(legend.position = "none") +
labs(y = "bitterness (IBU)",
x = "alcohol volume (ABV)",
title = "Craft beers from American breweries")

ggplotly(p)
```

### Overlaid Points
I use variable "style2" to filter out the six most common beer styles. This way, we can see that the cluster of beers in the top right (i.e. more bitter and higher alcohol content) are IPAs - perhaps unsurprisingly.

```{r}
library(plotly)
library(dplyr)
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)

p <- ggplot(beers, aes(x=abv, y=ibu)) +
geom_density2d(alpha=0.5) +
geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) +
labs(y = "bitterness (IBU)",
x = "alcohol volume (ABV)",
title = "Craft beers from American breweries",
colour = "Beer types")

ggplotly(p)
```

313 changes: 0 additions & 313 deletions ggplot2/2021-08-04-2D-Histogram.md

This file was deleted.

Loading