Skip to content

Commit 2fc741e

Browse files
New ggplot2 docs (#93)
* New docs Removed old ones and improved ones. * new docs fix * fix * Update 2021-08-04-bar-charts.Rmd fix * Update DESCRIPTION adding missing packages * Update DESCRIPTION adding missing packages * Update DESCRIPTION * Update DESCRIPTION * Added ggnet * adding packages * Update DESCRIPTION * Update DESCRIPTION * Update DESCRIPTION * Update DESCRIPTION Co-authored-by: HammadTheOne <[email protected]>
1 parent 4b7f0d5 commit 2fc741e

File tree

163 files changed

+8273
-22545
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+8273
-22545
lines changed

.circleci/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
sudo R -e 'devtools::install_github("hypertidy/[email protected]", dependencies = TRUE) '
3030
sudo R -e 'devtools::install_github("plotly/dash-daq", dependencies = TRUE, upgrade = TRUE) '
3131
sudo R -e 'devtools::install_deps(dependencies = TRUE) '
32+
sudo R -e 'devtools::install_github("briatte/ggnet", dependencies = TRUE) '
3233
- save_cache:
3334
key: cache4
3435
paths:

DESCRIPTION

+14-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ Imports:
5959
nlme,
6060
Lahman,
6161
quantreg,
62-
backports
62+
backports,
63+
mapproj,
64+
ggrepel,
65+
ggdendro,
66+
treemapify,
67+
GGally,
68+
tree,
69+
ggfortify,
70+
cluster,
71+
hrbrthemes,
72+
ggQC,
73+
fmsb,
74+
plotROC,
75+
tidyquant

ggplot2/2021-08-04-2D-Histogram.Rmd

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
name: 2D-Histogram
3+
permalink: ggplot2/geom_density2d/
4+
description: How to make 2D-Histogram Plots plots in ggplot2 with Plotly.
5+
layout: base
6+
thumbnail: thumbnail/histogram2d.jpg
7+
language: ggplot2
8+
page_type: u-guide
9+
display_as: statistical
10+
order: 5
11+
output:
12+
html_document:
13+
keep_md: true
14+
---
15+
16+
```{r, echo = FALSE, message=FALSE}
17+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
18+
```
19+
### Basic 2D Graph
20+
Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data)
21+
22+
```{r}
23+
library(plotly)
24+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
25+
26+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
27+
geom_density2d() +
28+
labs(y = "bitterness (IBU)",
29+
x = "alcohol volume (ABV)",
30+
title = "Craft beers from American breweries")
31+
32+
ggplotly(p)
33+
```
34+
35+
### Filled
36+
Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph.
37+
38+
```{r}
39+
library(plotly)
40+
41+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
42+
43+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
44+
stat_density2d(aes(fill = stat(level)), geom="polygon") +
45+
labs(y = "bitterness (IBU)",
46+
x = "alcohol volume (ABV)",
47+
title = "Craft beers from American breweries")
48+
49+
ggplotly(p)
50+
```
51+
52+
### Preset Colourscale
53+
["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").
54+
55+
```{r}
56+
library(plotly)
57+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
58+
59+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
60+
stat_density2d(aes(fill = stat(level)), geom="polygon") +
61+
scale_fill_viridis_c(option = "plasma") +
62+
theme(legend.position = "magma") +
63+
labs(y = "bitterness (IBU)",
64+
x = "alcohol volume (ABV)",
65+
title = "Craft beers from American breweries")
66+
67+
ggplotly(p)
68+
```
69+
70+
### Customized Colourscale
71+
You can also set your own colour gradients by defining a high and low point.
72+
```{r}
73+
library(plotly)
74+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
75+
76+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
77+
stat_density2d(aes(fill = stat(level)), geom="polygon") +
78+
scale_fill_gradient(low = "lightskyblue1", high = "darkred") +
79+
theme(legend.position = "none") +
80+
labs(y = "bitterness (IBU)",
81+
x = "alcohol volume (ABV)",
82+
title = "Craft beers from American breweries")
83+
84+
ggplotly(p)
85+
```
86+
87+
### Overlaid Points
88+
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.
89+
90+
```{r}
91+
library(plotly)
92+
library(dplyr)
93+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
94+
95+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
96+
geom_density2d(alpha=0.5) +
97+
geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) +
98+
labs(y = "bitterness (IBU)",
99+
x = "alcohol volume (ABV)",
100+
title = "Craft beers from American breweries",
101+
colour = "Beer types")
102+
103+
ggplotly(p)
104+
```
105+

ggplot2/2021-08-04-2D-Histogram.md

-313
This file was deleted.

0 commit comments

Comments
 (0)