Repository navigation
Expand file tree
/
Copy pathpaper.R
More file actions
174 lines (131 loc) · 6.59 KB
/
Copy pathpaper.R
File metadata and controls
174 lines (131 loc) · 6.59 KB
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
# code to generate figures for paper
# run this code from ~/research/jmac/composition so use packrat controlled packages
paperDir <- '~/research/jmac/composition_paper'
source("config")
source(file.path(codeDir, "plot.R"))
source(file.path(codeDir, "prep_plot_data.R"))
source(file.path(codeDir, "set_domain.R"))
require(ncdf4)
require(ggplot2)
require(maptools)
require(rgdal)
require(raster)
require(gridExtra)
# for distinguishing no trees from no data
west_presence <- read.csv(file.path(dataDir, 'western.csv'))
west_presence <- west_presence[west_presence$x <= easternLimitOfWesternDomain, ]
west_presence$data <- as.numeric(rowSums(west_presence[ , c(3:ncol(west_presence))]) > 0)
# get in same order as data for fitting (from far N, W to E in rows)
tmp <- matrix(west_presence$data, length(westernDomainY), length(westernDomainX))
west_presence <- c(t(tmp[rev(seq_along(westernDomainY)), ]))
west_presence_fort <- west_fort
west_presence_fort$id <- as.numeric(west_presence_fort$id)
west_presence_fort <- cbind(west_presence_fort, west_presence[west_presence_fort$id])
names(west_presence_fort)[ncol(west_presence_fort)] <- "data"
east_presence_fort <- east_fort
east_presence_fort$data <- as.numeric(rowSums(east_presence_fort[ , 8:ncol(east_presence_fort)], na.rm = TRUE) > 0)
fort_presence <- rbind(west_presence_fort[ , c(1:7, ncol(west_presence_fort))], east_presence_fort[ , c(1:7, ncol(east_presence_fort))])
#########################################################################################
## Fig. 1
#########################################################################################
presence_long <- melt(fort_presence, c('X', 'Y', 'order', 'hole', 'piece', 'group', 'id'))
presence_long <- presence_long[presence_long$value == 1, ]
col = gray(c(.2,.5,.8))
d <- ggplot(presence_long, aes(X, Y, group = id)) + geom_polygon(aes(fill = as.factor(value))) +
scale_fill_manual(values = col, guide = FALSE)
d <- add_map_albers(plot_obj = d, map_data = paleonFortified, dat = presence_long)
d <- d + scale_y_continuous(limits = c(70000, 1490000))
d <- theme_clean(d)
setwd(paperDir)
pdf('fig1.pdf', width=4.85, height=3)
#postscript('fig1.eps', width=4.85, height=3)
print(d)
dev.off()
###########################################################################
focalTaxa <- c('Beech', 'Cherry', 'Chestnut', 'Elm', 'Hemlock', 'Oak', 'Pine')
focalTaxa <- focalTaxa[-2]
propBreaks = c(0, 0.01, 0.05, 0.10, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1)
figHgt = 16
figWth = 22
figHgtIndiv = 16*.5
figWthIndiv = 22*.5
psd[psd > .25] = 0.25
psdBreaks = c(0, 0.01, 0.03, 0.05, 0.075, 0.10, 0.15, 0.2, 0.25)
cv = psd/pm
cv[cv > 1.4] = 1.4
cvBreaks = seq(0, 1.4, by = 0.2)
figs <- list()
length(figs) <- length(focalTaxa) * 3
cnt <- 1
for( taxon in focalTaxa ) {
figs[[cnt]] <- make_areal_map(data = taxon_dat_long, variables = taxon, breaks = propBreaks, legendName = 'raw proportions', map_data = paleonFortified, facet = FALSE, ncol = 1, legend = FALSE, title = FALSE) + theme(plot.margin = unit(rep(0,4), 'lines'))
figs[[cnt + 1]] <- make_veg_map(data = pm[ , taxon, drop = FALSE], breaks = propBreaks, coords = coord, legendName = 'fitted proportions', map_data = paleonFortified, facet = FALSE, ncol = 1, legend = FALSE) + theme(plot.margin = unit(rep(0,4), 'lines'))
figs[[cnt + 2]] <- make_veg_map(data = psd[ , taxon, drop = FALSE], breaks = psdBreaks, coords = coord, legendName = 'standard error', map_data = paleonFortified, facet = FALSE, ncol = 1, legend = FALSE, col = heat.colors, title = FALSE) + theme(plot.margin = unit(rep(0,4), 'lines'))
# figs[[cnt + 2]] <- make_veg_map(data = cv[ , taxon, drop = FALSE], breaks = cvBreaks, coords = coord, legendName = 'standard error', map_data = paleonFortified, facet = FALSE, ncol = 1, legend = FALSE, col = heat.colors, title = FALSE) + theme(plot.margin = unit(rep(0,4), 'lines'))
cnt <- cnt + 3
}
setwd(paperDir)
pdf('fig2.pdf', width=7, height=10)
#postscript('fig2.eps', width=7, height=10)
do.call(grid.arrange, c(figs, nrow = length(focalTaxa), ncol = 3))
dev.off()
scaling = .5
ix=1
iy=seq(0,1,len=300)
iz=matrix(iy,nc=300)
pdf(file='legendRaw.pdf',width=5/scaling,height=.7/scaling,paper="special")
#postscript(file='legendRaw.eps',width=5/scaling,height=.7/scaling,paper="special")
par(fig=c(0,1,0,1),mai=c(0.6,.2,0,.3),mgp=c(1.8,.7,0),cex.axis=1.5)
cols <- rev(terrain.colors(length(propBreaks)-1))
cols[1] <- terrain.colors(40)[39]
image(iy,ix,t(iz), yaxt = "n", xlab = "", xaxt='n',
ylab = "", col = cols, bty= 'n')
axis(1, at = seq(0,1, len = length(propBreaks)), labels = as.character(propBreaks),
cex.axis = 1.8, tick = FALSE)
box()
dev.off()
ix=1
iy=seq(0,1,len=300)
iz=matrix(iy,nc=300)
pdf(file='legendProp.pdf',width=5/scaling,height=.7/scaling,paper="special")
#postscript(file='legendProp.eps',width=5/scaling,height=.7/scaling,paper="special")
par(fig=c(0,1,0,1),mai=c(0.6,.2,0,.3),mgp=c(1.8,.7,0),cex.axis=1.5)
image(iy,ix,t(iz), yaxt = "n", xlab = "", xaxt='n',
ylab = "", col = rev(terrain.colors(length(propBreaks)-1)))
axis(1, at = seq(0,1, len = length(propBreaks)), labels = as.character(propBreaks),
cex.axis = 1.8, tick = FALSE)
box()
dev.off()
ix=1
iy=seq(0,1,len=300)
iz=matrix(iy,nc=300)
pdf(file='legendSD.pdf',width=5/scaling,height=.7/scaling,paper="special")
# postscript(file='legendSD.eps',width=5/scaling,height=.7/scaling,paper="special")
par(fig=c(0,1,0,1),mai=c(0.6,.2,0,.3),mgp=c(1.8,.7,0),cex.axis=1.5)
image(iy,ix,t(iz), yaxt = "n", xlab = "", xaxt='n',
ylab = "", col = rev(heat.colors(length(psdBreaks)-1)))
axis(1, at = seq(0,1, len = length(psdBreaks)), labels = as.character(psdBreaks),
cex.axis = 1.8, tick = FALSE)
box()
dev.off()
ix=1
iy=seq(0,1,len=300)
iz=matrix(iy,nc=300)
pdf(file='legendCV.pdf',width=5/scaling,height=.7/scaling,paper="special")
# postscript(file='legendSD.eps',width=5/scaling,height=.7/scaling,paper="special")
par(fig=c(0,1,0,1),mai=c(0.6,.2,0,.3),mgp=c(1.8,.7,0),cex.axis=1.5)
image(iy,ix,t(iz), yaxt = "n", xlab = "", xaxt='n',
ylab = "", col = rev(heat.colors(length(cvBreaks)-1)))
axis(1, at = seq(0,1, len = length(cvBreaks)), labels = as.character(cvBreaks),
cex.axis = 1.8, tick = FALSE)
box()
dev.off()
# Fig 3
# take plot.full code with appropriate # cols passed to make_veg_map
propBreaks = c(0, 0.01, 0.05, 0.10, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1)
figHgt = 11
figWth = 8.5
pdf(file = 'fig3.pdf', height = figHgt, width = figWth)
#postscript(file = 'fig3.eps', height = figHgt, width = figWth)
make_veg_map(data = pm, breaks = propBreaks, coords = coord, legendName = 'fitted proportions', map_data = paleonFortified, facet = TRUE, ncol = 3)
dev.off()