-
Notifications
You must be signed in to change notification settings - Fork 0
/
more_ggplots.Rmd
170 lines (123 loc) · 4.26 KB
/
more_ggplots.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# More ggplots
## Histograms and density plots
### Histogram
We can use one of our previous objects to create a histogram. You can get it back with:
```{r}
rnaseq2 <- read_csv("DataViz_source_files-main/files/GSE150029_rnaseq_log2_long.csv")
```
```{r}
ggplot(rnaseq2, aes(x=log2_counts)) +
geom_histogram()
```
We can split the histogram per sample:
```{r}
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram()
```
Set position to "identity", so histograms will not be on top of each other:
```{r}
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(position="identity")
```
Set alpha (transparency to 0.2):
```{r}
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(position="identity", alpha=0.2)
```
Express as density instead of counts:
```{r}
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(aes(y=after_stat(density)), position="identity", alpha=0.2) +
theme_classic()
```
### Density plot
The structure is pretty much the same:
```{r}
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_density(alpha=0.2) +
theme_classic()
```
You can use fill instead:
```{r}
ggplot(rnaseq2, aes(x=log2_counts, fill=sample)) +
geom_density(alpha=0.2) +
theme_classic()
```
### Histogram + density
As we combined geom_boxplot and geom_violin in a previous exercise, we can also combine geom_histogram and geom_density:
```{r}
ggplot(rnaseq2, aes(x=log2_counts, color=sample)) +
geom_histogram(aes(y=after_stat(density)), position="identity", alpha=0.2) +
geom_density(alpha=0.2) +
theme_classic()
```
## Pie chart
We will plot a pie chart representing the number of genes from **gtf** found in each chromosome.
Prepare data so as to obtain counts (that is more advanced dplyr manipulation - we will not go through details):
```{r}
gtf_count_chr <- gtf %>%
group_by(chr) %>% # group by allows to make the next calculations by the group specified
summarise(counts=n())
```
The **coord_polar()** is needed here: pie plots are stacked barplots in polar coordinates.
```{r}
ggplot(data=gtf_count_chr, mapping=aes(x="", y=counts, fill=chr)) +
geom_bar(stat="identity") +
coord_polar("y", start=0)
```
```{r}
ggplot(data=gtf_count_chr, mapping=aes(x="", y=counts, fill=chr)) +
geom_bar(stat="identity") +
coord_polar("y", start=0) +
theme_void()
```
Change the color scale:
```{r}
ggplot(data=gtf_count_chr, mapping=aes(x="", y=counts, fill=chr)) +
geom_bar(stat="identity") +
coord_polar("y", start=0) +
scale_fill_brewer(palette="Paired") +
theme_void()
```
Here is a [nice page](https://genchanghsu.github.io/ggGallery/posts/2021-06-18-post-3-pie-charts-with-ggplot/) about more pie plot customization.
## Marginal plots
Function [**ggMarginal**](https://www.rdocumentation.org/packages/ggExtra/versions/0.10.1/topics/ggMarginal) from package {ggExtra} allows to easily add histograms, boxplots or density plots to the margins of a scatter plot.
```{r}
library(ggExtra)
# Save the classic plot to an object.
p <- ggplot(data=geneexp, mapping=aes(x=sample1, y=sample2)) +
geom_point()
```
```{r}
# add marginal histogram
ggMarginal(p, type="histogram")
```
```{r}
# add marginal density
ggMarginal(p, type="density")
```
```{r}
# add marginal boxplot
ggMarginal(p, type="boxplot")
```
The **mapping of variables** (for example, using **color** or **fill**) can also be **inherited** by the marginal plots.
```{r}
# add mapping of points to "DE" column :
p <- ggplot(data=geneexp, mapping=aes(x=sample1, y=sample2, color=DE)) +
geom_point()
# tell ggMarginal to color / split the boxplot according to variable mapped in color/colour
ggMarginal(p, type="boxplot", groupColour = TRUE)
```
Let's add more features that we have learned in this workshop:
```{r, fig.height=12, fig.width=10}
# add mapping of points to "DE" column :
p <- ggplot(data=geneexp, mapping=aes(x=sample1, y=sample2, color=DE)) +
geom_point() +
theme_minimal(base_size = 16) +
geom_rug(alpha=0.4, sides = "tr") +
geom_density_2d(alpha=0.2, colour = "pink") +
ggtitle("Example of ggMarginal") +
theme(legend.position="bottom", plot.title = element_text(hjust = 0.5))
# tell ggMarginal to color / split the boxplot according to variable mapped in color/colour
ggMarginal(p, type="boxplot", groupColour = TRUE)
```