generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 67
/
add_tutorial.Rmd
123 lines (95 loc) · 3.75 KB
/
add_tutorial.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
113
114
115
116
117
118
119
120
121
122
123
# Radial bar chart and other interesting graphs
Zhe Hou
```{r, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
```{r}
library(ggplot2)
library(reshape2)
library(ggradar)
```
## Radial Bar Chart
### Overview
Radial Bar Chart is a kind of graph that display bars redially, and all bars are standing on a circle.
### full-fledged example
Here is a full-fledged example of Radial Bar Chart.
```{r}
# generate data
df <- data.frame(name=rep(LETTERS[1:10], 5),
subject=rep(c("Chinese", "English", "History", "Biology", "Music"), each=10),
score=rep((1:5), each=10) + rnorm(50, 0, 0.5))
df$subject <- factor(df$subject, levels = c("Chinese", "English", "History", "Biology", "Music"))
# plot
ggplot(data=df,aes(name,score,fill=subject))+
geom_bar(stat="identity", color="black", position=position_dodge(),width=0.65,size=0.3)+
coord_polar(theta = "x",start=0) +
ylim(c(-1,6))+
scale_fill_brewer(palette="YlGnBu")+
theme_light()+
theme( axis.text.y = element_text(size = 12,colour="black"),
axis.text.x=element_text(size = 10,colour="black"))
```
### Simple Example
```{r}
# generate a simpler dataset
sim_data <- data.frame( a=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),b=c(70, 50, 60, 30,100,90,40))
sim_data$a <- factor(sim_data$a, levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
# plot
ggplot(sim_data) +
geom_bar(aes(x=a, y=b), width = 1, stat="identity",
colour = "black", fill="lightblue") +
coord_polar(theta = "x", start=0)
```
This is a single variable radial bar chart, which displays temperature of every day in one week. "coord_polar()" is key to let the ordinary bar chart become radial bar chart.
```{r}
# generate data
df <- data.frame(name=rep(LETTERS[1:10], 5),
subject=rep(c("Chinese", "English", "History", "Biology", "Music"), each=10),
score=rep((1:5), each=10) + rnorm(50, 0, 0.5))
df$subject <- factor(df$subject, levels = c("Chinese", "English", "History", "Biology", "Music"))
# plot
ggplot(data=df,aes(name,score,fill=subject))+
geom_bar(stat="identity", position=position_dodge())+
coord_polar(theta = "x",start=0)
```
When we have two variables, we should set the parameter "positon". But now we can see that all bars are start from a point, which is not easy to observe small bars.
```{r}
# plot
ggplot(data=df,aes(name,score,fill=subject))+
geom_bar(stat="identity", position=position_dodge())+
coord_polar(theta = "x",start=0) +
ylim(c(-2,6))+
theme_light()
```
We can set the y-axis start from -2 (so -2 is the center point of circles), and now we can clearly observe bars.
## Other interesting graphs
### Waffle Chart
```{r}
# prepare data
table <- round(table(mpg$class ) * (100/(length(mpg$class))))
sorted_table<-sort(table,index.return=TRUE,decreasing = FALSE)
Order<-sort(as.data.frame(table)$Freq,index.return=TRUE,decreasing = FALSE)
df <- expand.grid(y = 1:10, x = 1:10)
df$category<-factor(rep(names(sorted_table),sorted_table), levels=names(sorted_table))
# plot
ggplot(df, aes(x = y, y = x, fill = category)) +
geom_tile(color = "white", size = 0.25) +
coord_fixed(ratio = 1)+
scale_y_continuous(trans = 'reverse') +
theme( panel.background = element_blank(),
plot.title = element_text(size = rel(1.2)),
legend.position = "right")
```
This is a Waffle chart, which shows the percentage of each category of cars.
### Redar Graph
```{r}
# prepare data
a <- matrix(runif(10, 30, 100)/100, 2, 5)
colnames(a) <- c("Chinese", "English", "History", "Biology", "Music")
df <- data.frame(a)
name <- c("John", "Lily")
df <- data.frame(name, df)
# plot
ggradar(df)
```
This is a redar graph which displays scores of some subjects of two students.