Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fe87a2

Browse files
authored
Merge pull request plotly#1405 from plotly/your_feature_branch
Adding a new ggplot page (geom_bin2d)
2 parents 0dbe107 + 58b8ab0 commit 9fe87a2

16 files changed

+432
-88
lines changed

_posts/ggplot2/2016-11-29-geom_tile.Rmd

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ library(reshape2)
4646
4747
p <- volcano %>%
4848
melt() %>%
49-
ggplot(aes(Var1, Var2, fill = value)) + geom_tile()
49+
ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
50+
labs(x = "West to East",
51+
y="North to South",
52+
title = "Visualizing the Topography of Maunga Whau")
5053
5154
p <- ggplotly(p)
5255
@@ -97,20 +100,19 @@ chart_link
97100
```
98101
Inspired by <a href="http://stackoverflow.com/questions/25528059/cluster-data-in-heat-map-in-r-ggplot">Stack Overflow</a>
99102

100-
### Continuous Colorscale
103+
### Modified Colorscale
101104

102105
```{r, results='hide'}
103106
library(plotly)
104107
library(reshape2)
105108
106109
p <- volcano %>%
107110
melt() %>%
108-
ggplot(aes(Var1, Var2, fill = value)) + geom_tile()
109-
110-
p <- plotly_build(p)
111-
112-
# Modify the plotly abject to add colorscale
113-
p$data[[1]]$colorscale = list(c(0, "#ffe6cc"), c(1, "#ff8400"))
111+
ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
112+
scale_fill_gradient(low="#ffe6cc", high="#ff8400") +
113+
labs(x = "West to East",
114+
y="North to South",
115+
title = "Visualizing the Topography of Maunga Whau")
114116
115117
p <- ggplotly(p)
116118

_posts/ggplot2/2016-11-29-geom_tile.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ packageVersion('plotly')
3636
```
3737

3838
```
39-
## [1] '4.5.6.9000'
39+
## [1] '4.9.0'
4040
```
4141

4242
### Matrix
@@ -48,7 +48,10 @@ library(reshape2)
4848

4949
p <- volcano %>%
5050
melt() %>%
51-
ggplot(aes(Var1, Var2, fill = value)) + geom_tile()
51+
ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
52+
labs(x = "West to East",
53+
y="North to South",
54+
title = "Visualizing the Topography of Maunga Whau")
5255

5356
p <- ggplotly(p)
5457

@@ -96,7 +99,7 @@ chart_link
9699
<iframe src="https://plot.ly/~RPlotBot/4177.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
97100
Inspired by <a href="http://stackoverflow.com/questions/25528059/cluster-data-in-heat-map-in-r-ggplot">Stack Overflow</a>
98101

99-
### Continuous Colorscale
102+
### Modified Colorscale
100103

101104

102105
```r
@@ -105,12 +108,11 @@ library(reshape2)
105108

106109
p <- volcano %>%
107110
melt() %>%
108-
ggplot(aes(Var1, Var2, fill = value)) + geom_tile()
109-
110-
p <- plotly_build(p)
111-
112-
# Modify the plotly abject to add colorscale
113-
p$data[[1]]$colorscale = list(c(0, "#ffe6cc"), c(1, "#ff8400"))
111+
ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
112+
scale_fill_gradient(low="#ffe6cc", high="#ff8400") +
113+
labs(x = "West to East",
114+
y="North to South",
115+
title = "Visualizing the Topography of Maunga Whau")
114116

115117
p <- ggplotly(p)
116118

_posts/ggplot2/2017-04-21-geom_quantile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ packageVersion('plotly')
3636
```
3737

3838
```
39-
## [1] '4.7.0'
39+
## [1] '4.9.0'
4040
```
4141

4242
### Basic Example
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: geom_bin2d | Examples | Plotly
3+
name: geom_bin2d
4+
permalink: ggplot2/geom_bin2d/
5+
description: How to make a 2-dimensional heatmap in ggplot2 using geom_bin2d. Examples of coloured and facetted graphs.
6+
layout: base
7+
thumbnail: thumbnail/geom_bin2d.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: statistical
12+
order: 2
13+
output:
14+
html_document:
15+
keep_md: true
16+
---
17+
18+
```{r, echo = FALSE, message=FALSE}
19+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
20+
Sys.setenv("plotly_username"="RPlotBot")
21+
Sys.setenv("plotly_api_key"="q0lz6r5efr")
22+
```
23+
24+
### New to Plotly?
25+
26+
Plotly's R library is free and open source!<br>
27+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
28+
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.<br>
29+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
30+
31+
### Version Check
32+
33+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
34+
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.
35+
36+
```{r}
37+
library(plotly)
38+
packageVersion('plotly')
39+
```
40+
41+
### Basic 2d Heatmap
42+
43+
```{r, results='hide'}
44+
library(plotly)
45+
46+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
47+
48+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
49+
geom_bin2d() +
50+
labs(title = "Distribution of Canadian areas by English and French fluency",
51+
x = "% fluent in English",
52+
y = "% fluent in French",
53+
fill = "# of census \nsubdivisions")
54+
p <- ggplotly(p)
55+
56+
# Create a shareable link to your chart
57+
# Set up API credentials: https://plot.ly/r/getting-started
58+
chart_link = api_create(p, filename="geom_bin2d/2d-chart")
59+
chart_link
60+
```
61+
62+
```{r echo=FALSE}
63+
chart_link
64+
```
65+
66+
### Customized Colours
67+
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.
68+
69+
```{r, results='hide'}
70+
library(plotly)
71+
72+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
73+
geom_bin2d() +
74+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
75+
labs(title = "Distribution of Canadian towns by English and French fluency",
76+
x = "% fluent in English",
77+
y = "% fluent in French",
78+
fill = "# of census \nsubdivisions")
79+
p <- ggplotly(p)
80+
81+
# Create a shareable link to your chart
82+
# Set up API credentials: https://plot.ly/r/getting-started
83+
chart_link = api_create(p, filename="geom_bin2d/log-chart")
84+
chart_link
85+
```
86+
87+
```{r echo=FALSE}
88+
chart_link
89+
```
90+
91+
### Weighted Data
92+
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.
93+
94+
```{r, results='hide'}
95+
library(plotly)
96+
97+
p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
98+
geom_bin2d() +
99+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
100+
labs(title = "Distribution of the Canadian population by English and French fluency",
101+
x = "% fluent in English",
102+
y = "% fluent in French",
103+
fill = "# of people")
104+
p <- ggplotly(p)
105+
106+
107+
# Create a shareable link to your chart
108+
# Set up API credentials: https://plot.ly/r/getting-started
109+
chart_link = api_create(p, filename="geom_bin2d/weighted-data")
110+
chart_link
111+
```
112+
113+
```{r echo=FALSE}
114+
chart_link
115+
```
116+
117+
### With Facets
118+
We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides.
119+
120+
```{r, results='hide'}
121+
library(plotly)
122+
123+
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
124+
geom_bin2d(bins = 20) +
125+
facet_wrap(~factor(region, levels = c("Atlantic","Québec","Ontario","Prairies","British Columbia"))) +
126+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
127+
labs(title = "Distribution of Canadian towns by English and French fluency",
128+
x = "% fluent in English",
129+
y = "% fluent in French",
130+
fill = "# of people")
131+
p <- ggplotly(p)
132+
133+
134+
# Create a shareable link to your chart
135+
# Set up API credentials: https://plot.ly/r/getting-started
136+
chart_link = api_create(p, filename="geom_bin2d/with-facet")
137+
chart_link
138+
```
139+
140+
```{r echo=FALSE}
141+
chart_link
142+
```
143+
144+
### Customized Appearance
145+
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.
146+
147+
```{r, results='hide'}
148+
library(plotly)
149+
150+
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
151+
geom_bin2d(bins = 20) +
152+
facet_wrap(~factor(region, levels = c("Atlantic","Québec","Ontario","Prairies","British Columbia"))) +
153+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
154+
labs(title = "Distribution of Canadian towns by English and French fluency",
155+
x = "% fluent in English",
156+
y = "% fluent in French",
157+
fill = "# of people") +
158+
theme_bw() +
159+
theme(text = element_text(family = 'Fira Sans'))
160+
p <- ggplotly(p)
161+
162+
163+
# Create a shareable link to your chart
164+
# Set up API credentials: https://plot.ly/r/getting-started
165+
chart_link = api_create(p, filename="geom_bin2d/customize-theme")
166+
chart_link
167+
```
168+
169+
```{r echo=FALSE}
170+
chart_link
171+
```
172+

0 commit comments

Comments
 (0)