-
Notifications
You must be signed in to change notification settings - Fork 230
/
27-levelplot-with-lattice.Rmd
235 lines (133 loc) · 4.52 KB
/
27-levelplot-with-lattice.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
title: "Heatmap with lattice and levelplot()"
descriptionMeta: "This document explains how to use the levelplot() function of the lattice R package to build heatmaps."
descriptionTop: "This document explains how to use the `levelplot()` function of the `lattice` R package to build [heatmaps](heatmap.html)."
sectionText: "Heatmap section"
sectionLink: "heatmap.html"
DataToVizText: "Data to Viz"
DataToVizLink: "data-to-viz.com/graph/heatmap.html"
url: "27-levelplot-with-lattice"
output:
html_document:
self_contained: false
mathjax: default
lib_dir: libs
template: template_rgg.html
css: style.css
toc: TRUE
toc_float: TRUE
toc_depth: 2
df_print: "paged"
---
```{r global options, include = FALSE}
knitr::opts_chunk$set( warning=FALSE, message=FALSE)
```
<div class="container">
# Basis use of levelplot()
***
<div class = "row">
<div class = "col-md-6 col-sm-12 align-self-center">
The `lattice` package allows to build [heatmaps](heatmap.html) thanks to the `levelplot()` function.
<u>Input data</u>: here input is a data frame with 3 columns prividing the X and Y coordinate of the cell and its value. (<u>Long</u> format).
</div>
<div class = "col-md-6 col-sm-12">
```{r thecode, echo=FALSE, out.width = "100%", fig.height=7}
# Load the lattice package
library("lattice")
# Dummy data
x <- seq(1,10, length.out=20)
y <- seq(1,10, length.out=20)
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, 0, 5)
## Try it out
levelplot(Z ~ X*Y, data=data ,xlab="X",
main="")
```
</div>
</div>
```{r thecode, eval=FALSE}
```
# From wide input matrix
***
<div class = "row">
<div class = "col-md-6 col-sm-12 align-self-center">
Previous example of this document was based on a data frame at the long format. Here, a square matrix is used instead. It is the second format understood by the `levelplot()` function.
<u>Note</u>: here row and column order isn't respected in the heatmap.
</div>
<div class = "col-md-6 col-sm-12">
```{r thecode3, echo=FALSE, out.width = "100%", fig.height=7}
# Load the library
library("lattice")
# Dummy data
data <- matrix(runif(100, 0, 5) , 10 , 10)
colnames(data) <- letters[c(1:10)]
rownames(data) <- paste( rep("row",10) , c(1:10) , sep=" ")
# plot it flipping the axis
levelplot(data)
```
</div>
</div>
```{r thecode3, eval=FALSE}
```
# Flip and reorder axis
***
<div class = "row">
<div class = "col-md-6 col-sm-12 align-self-center">
The `t()` function of R allows to transpose the input matrix, and thus to flip X and Y coordinates.
Moreover, you can reverse matrix order as shown below to reverse order in the heatmap as well. Now the heatmap is organized exactly as the input matrix.
</div>
<div class = "col-md-6 col-sm-12">
```{r thecode2, echo=FALSE, out.width = "100%", fig.height=7}
# Load the library
library("lattice")
# Dummy data
data <- matrix(runif(100, 0, 5) , 10 , 10)
colnames(data) <- letters[c(1:10)]
rownames(data) <- paste( rep("row",10) , c(1:10) , sep=" ")
# plot it flipping the axis
levelplot( t(data[c(nrow(data):1) , ]),
col.regions=heat.colors(100))
```
</div>
</div>
```{r thecode2, eval=FALSE}
```
# Custom colors
***
```{r thecode4, echo=FALSE, out.width = c("30%","30%","30%"), fig.height=9, fig.show="hold"}
# Lattice package
require(lattice)
# The volcano dataset is provided, it looks like that:
#head(volcano)
# 1: native palette from R
levelplot(volcano, col.regions = terrain.colors(100)) # try cm.colors() or terrain.colors()
# 2: Rcolorbrewer palette
library(RColorBrewer)
coul <- colorRampPalette(brewer.pal(8, "PiYG"))(25)
levelplot(volcano, col.regions = coul) # try cm.colors() or terrain.colors()
# 3: Viridis
library(viridisLite)
coul <- viridis(100)
levelplot(volcano, col.regions = coul)
#levelplot(volcano, col.regions = magma(100))
```
<div class = "row">
<div class = "col-md-6 col-sm-12 align-self-center">
There are several ways to custom the color palette:
- native palettes of R: `terrain.color()`, `rainbow()`, `heat.colors()`, `topo.colors()` or `cm.colors()`
- palettes of `RColorBrewer`. See list of available palettes [here](package/rColorBrewer.html).
- palettes of `Viridis`: viridis, magma, inferno, plasma.
</div>
<div class = "col-md-6 col-sm-12">
```{r, echo=FALSE, fig.width=5, fig.height=3, fig.align="center"}
levelplot(volcano, col.regions = magma(100))
```
</div>
</div>
```{r thecode4, eval=FALSE}
```
<!-- Close container -->
</div>
```{r, echo=FALSE}
htmltools::includeHTML("htmlChunkRelatedCorrelation.html")
```