-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heatmap_ggplot.R
29 lines (22 loc) · 2.69 KB
/
Heatmap_ggplot.R
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
# Data
child.maxs = matrix(seq(1, 19, by = 2), nrow = 10, ncol = 15)
old.mins = matrix(61:75, nrow = 10, ncol = 15, byrow = T)
Hm = matrix(c( 0.2043571, 0.2226860, 0.2464833, 0.2482801, 0.2415724, 0.2363867, 0.2340365, 0.2319989, 0.2378935, 0.20435710, 0.2043569, 0.2227047, 0.2464832, 0.2482802, 0.2415722, 0.2363823, 0.2340111, 0.2319991, 0.2378941, 0.20435690, 0.2043569, 0.2226909, 0.2464827, 0.2482797, 0.2415737, 0.2363817, 0.2340110, 0.2319989, 0.2378890, 0.20435690, 0.2043577, 0.2227046, 0.2464833, 0.2482801, 0.2415724, 0.2363817, 0.2340097, 0.2319984, 0.2378936, 0.20435770, 0.2043583, 0.2227072, 0.2464831, 0.2482801, 0.2415726, 0.2363844, 0.2340109, 0.2319938, 0.2378938, 0.20435830, 0.2043582, 0.2227044, 0.2464167, 0.2482771, 0.2415723, 0.2363835, 0.2340107, 0.2319904, 0.2378943, 0.20435820, 0.2043580, 0.2227043, 0.2464842, 0.2482803, 0.2415720, 0.2363830, 0.2340127, 0.2319966, 0.2378934, 0.20435800, 0.2043580, 0.2227033, 0.2465037, 0.2482801, 0.2415720, 0.2363830, 0.2340093, 0.2319957, 0.2378933, 0.20435800, 0.2043587, 0.2227049, 0.2464477, 0.2482801, 0.2415719, 0.2363824, 0.2340034, 0.2319990, 0.2378944, 0.20435870, 0.2043587, 0.2227051, 0.2464832, 0.2482825, 0.2415758, 0.2363828, 0.2340096, 0.2319949, 0.2378934, 0.20435870, 0.2043582, 0.2227006, 0.2464832, 0.2482801, 0.2415724, 0.2363845, 0.2340175, 0.2319990, 0.2378934, 0.20435820, 0.2043590, 0.2227071, 0.2464801, 0.2482801, 0.2415721, 0.2363833, 0.2340100, 0.2319990, 0.2378900, 0.20435900, 0.2043581, 0.2227051, 0.2464831, 0.2482801, 0.2415725, 0.2363834, 0.2340091, 0.2319992, 0.2378934, 0.20435810, 0.2043559, 0.2227051, 0.2464832, 0.2482809, 0.2410970, 0.2353516, 0.2332338, 0.2316103, 0.2376833, 0.20435590, 0.2012437, 0.2199822, 0.2437402, 0.2444422, 0.2372560, 0.2316670, 0.2295583, 0.2278882, 0.2338612, 0.20124370), nrow = 10, ncol = 15)
# Add row and column names. These will turn in to the X and Y labels in your plots
rownames(Hm) = child.maxs[,1]
colnames(Hm) = old.mins[1,]
## NOTE!! ##
# ggplot needs a data frame to make a heat map, so convert your data matrix to a data frame
library(reshape)
Hm.reformat = melt(Hm)
# Load packages
library(ggplot2)
# Plot using data in Hm.reformat, with y axis given by column X1 and x axis given by column X2 in data frame Hm.reformat
p <- ggplot(Hm.reformat, aes(y=X1,x=X2))
# Use plot p (created above) with the tile aesthetic (make a heatmap). Fill = value tells the plot to scale the color of each cell according to the data's value
p + geom_raster(aes(fill=value)) +
#Set the color scale
scale_fill_gradient(low="white", high="darkblue") +
# Add text next to each axis
## NOTE THAT THIS ORIENTATION IS DIFFERENT THAN WHEN USING image() IN THE OTHER EXAMPLE
xlab("adult.mins") + ylab("child.maxs")