8000 Updates to ggplot2 docs by danton267 · Pull Request #95 · plotly/plotly.r-docs · GitHub
[go: up one dir, main page]

Skip to content

Updates to ggplot2 docs #95

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 20 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
new docs fix
  • Loading branch information
danton267 committed Sep 27, 2021
commit 03564231814c9bd527c2b9305346166b098facd5
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
0