forked from ecoaspect/knitr-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbar-charts.Rmd
112 lines (82 loc) · 2.61 KB
/
bar-charts.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Bar Charts
========================================================
(See the source code and more examples here: https://github.com/plotly/knitr-demos)
In `ggplot2`, you plot bar charts using `geom_bar` plus `stat_identity`.
```{r}
library(ggplot2)
library(plotly)
library(devtools)
install_github("ropensci/plotly")
# Initialize a plotly object to sign in to your Plotly API account
py <- plotly('R-demos', 'p9g4f35ytd')
```
Basic Bar Chart
===
```{r, tidy=FALSE}
# Consider the following data frame
researchers <- data.frame(country=c("Canada", "Canada", "Germany", "USA"),
name=c("Warren", "Andreanne", "Stefan", "Toby"),
papers=c(23, 14, 37, 20),
field=c("Math", "Bio", "Bio", "Math"))
# Let us plot the number of papers (y) per name (x)
gg.basic <- ggplot(researchers, aes(x=name, y=papers)) +
geom_bar(stat="identity")
```
In R your plot looks like this:
```{r fig.width=7, fig.height=6}
print(gg.basic)
```
Now send it to Plotly!
```{r, plotly=TRUE, width=600, height=600}
py$ggplotly(gg.basic)
```
Grouped Bar Chart
===
```{r}
# Let us plot the number of papers (y) per country (x) splitting by field
gg.dodge <- ggplot(researchers, aes(x=country, y=papers, fill=field)) +
geom_bar(stat="identity", position="dodge")
```
In R your plot looks like this:
```{r fig.width=7, fig.height=6}
print(gg.dodge)
```
Send it to Plotly!
```{r, plotly=TRUE, width=600, height=600}
py$ggplotly(gg.dodge)
```
Oh, the default colors are different. Let us change the colors the way we like using Plotly's UI. Say we are happy with the following:
```{r, plotly=TRUE, width=600, height=600}
py$embed("https://plot.ly/~R-demos/11/papers-vs-country/")
```
Stacked Bar Chart
===
```{r}
# Let us plot the same thing but with a different layout (barmode='stack')
gg.stack <- ggplot(researchers, aes(x=country, y=papers, fill=field)) +
geom_bar(stat="identity", position="stack")
```
In R your plot looks like this:
```{r fig.width=7, fig.height=6}
print(gg.stack)
```
Send it to Plotly!
```{r, plotly=TRUE, width=600, height=600}
py$ggplotly(gg.stack)
```
Overlaid Bar Chart
===
```{r}
# Let us plot the same thing but with a different layout (barmode='overlay')
gg.identity <- ggplot(researchers, aes(x=country, y=papers, fill=field)) +
geom_bar(stat="identity", position="identity")
```
In R your plot looks like this:
```{r fig.width=7, fig.height=6}
print(gg.identity)
```
Send it to Plotly!
```{r, plotly=TRUE, width=600, height=600}
py$ggplotly(gg.identity)
```
You need to hover over the 'Canada' bar to see the info for 'Bio' (i.e., 14), which lies underneath.