-
Notifications
You must be signed in to change notification settings - Fork 2
/
02_missing_data_heatmap.R
65 lines (51 loc) · 2.43 KB
/
02_missing_data_heatmap.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
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
# ------------------------------------------------------------------------------
# Diallel heat maps (missing data visualization)
# S. Turner
# 06 July 2016
# ------------------------------------------------------------------------------
# Plots missing data by location, rep, and hybrid combination
library(ggplot2)
library(plyr)
setwd("~/GitHub/carrot-diallel/data")
# read in raw data
diallelRaw <- read.csv("diallelRawData.csv", header = TRUE)
str(diallelRaw)
# ------------------------------------------------------------------------------
# set, reorder, and rename factors
# ------------------------------------------------------------------------------
diallelRaw$female <- ordered(diallelRaw$female, levels = c("1", "2", "3", "4", "5", "6"))
diallelRaw$male <- ordered(diallelRaw$male, levels = c("6", "5", "4", "3", "2", "1"))
diallelRaw$year <- as.factor(diallelRaw$year)
diallelRaw$year <- revalue(diallelRaw$year, c("1" = "CA2015", "2" = "WI2015", "3" = "CA2016"))
diallelRaw$rep <- as.factor(diallelRaw$rep)
# ------------------------------------------------------------------------------
# take natural log for biomass
# ------------------------------------------------------------------------------
diallelRaw$lnFLW <- log(diallelRaw$flw)
diallelRaw$lnDLW <- log(diallelRaw$dlw)
diallelRaw$lnFRW <- log(diallelRaw$frw)
diallelRaw$lnDRW <- log(diallelRaw$drw)
# shoot:root ratio
diallelRaw$ratio <- diallelRaw$lnDLW/diallelRaw$lnDRW
# ------------------------------------------------------------------------------
# function to draw heatmaps
# ------------------------------------------------------------------------------
plotNA <- function(df, xname) {
p <- ggplot(df, aes(female, male)) +
geom_tile(aes_string(fill = xname), colour = "black") +
scale_fill_gradient(low = "light grey", high = "black", na.value = "white") +
geom_text(data = df[is.na(df[xname]),], aes(label = "NA", fontface = "bold")) +
facet_grid(year ~ rep, space = "free", scales = "free", labeller = label_both)
print(p)
}
# ------------------------------------------------------------------------------
# heatmaps for all traits
# ------------------------------------------------------------------------------
# extract trait names
traits <- c("midHeight", "midWidth", "height", "width", "flw",
"dlw", "frw", "drw", "ratio")
pdf("~/GitHub/carrot-diallel/results/missing_data_heatmap.pdf")
for (i in traits) {
plotNA(diallelRaw, i)
}
dev.off()