Commit 872f5934 authored by Gwen Iacona's avatar Gwen Iacona
Browse files

upload data and code used to generate figure

parents
This diff is collapsed.
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
## make a figure that shows how prioritization improves efficiency of action
## requested by reviewers for Science paper
library(ggplot2)
setwd("C:/Users/giacona/Dropbox/UQ Comp/Research/US PPP/documents/ESA SESYNC_shared/Manuscripts/Science Policy/Galleys/drafts/Figure 1")
AllOutput <- read.csv("./AllPPPOutput.csv", header = TRUE)
# drop the records where cost = 0 , or CE = NA
AllOutput1 <- AllOutput[-which(AllOutput$Co == 0),]
AllOutput1 <- AllOutput1[-which(is.na(AllOutput1$CE)),]
#dim(AllOutput) # 567 66
#dim(AllOutput1) # 544 66
#### Efficient Allocation
Efficient <- AllOutput1[order(-AllOutput1$CE),c("CE", "SpeciesInPlan", "Co")]
TotCo <- 0
TotSp <- 0
for (i in 1:length(Efficient$CE)){
TotCo <- TotCo + Efficient$Co[i]
TotSp <- TotSp + Efficient$SpeciesInPlan[i]
Efficient$TotCo[i] <- TotCo*0.000001
Efficient$TotSp[i] <- TotSp
}
head(Efficient)
### Random Allocation
Random <- AllOutput1[sample(length(AllOutput1[,1])),c("CE", "SpeciesInPlan", "Co")]
TotCoR <- 0
TotSpR <- 0
for (i in 1:length(Random$CE)){
TotCoR <- TotCoR + Random$Co[i]
TotSpR <- TotSpR + Random$SpeciesInPlan[i]
Random$TotCo[i] <- TotCoR*0.000001
Random$TotSp[i] <- TotSpR
}
head(Random)
### Random Allocation (sampled 100 times to obtain distribution)
simNum <- 50
# matrixes to hold simulated values.
#Each row is a different simulation and each column is all the records for a single simulation
RandCosts <- matrix(,nrow = simNum ,ncol = length(Random$CE))
RandSpecies <- matrix(,nrow = simNum,ncol = length(Random$CE))
RandData <- data.frame(matrix(,nrow = length(Random$CE), ncol=0))
for (j in 1:simNum){
Random <- AllOutput1[sample(length(AllOutput1[,1])),c("CE", "SpeciesInPlan", "Co")]
TotCoR <- 0
TotSpR <- 0
for (i in 1:length(Random$CE)){
TotCoR <- TotCoR + Random$Co[i]
TotSpR <- TotSpR + Random$SpeciesInPlan[i]
Random$TotCo[i] <- TotCoR*0.000001
Random$TotSp[i] <- TotSpR
}
RandCosts[j,] <- Random$TotCo
RandSpecies[j,] <- Random$TotSp
}
RandData$MeanCosts <- colMeans(RandCosts)
RandData$MeanSpp <- colMeans(RandSpecies)
RandData$Percentile5 <- apply(RandSpecies, 2, quantile, probs = 0.05)
RandData$Percentile95 <- apply(RandSpecies, 2, quantile, probs = 0.95)
# Fit a line to the mean value of each column to get the average of the sampling
#Set/calculate Values for vertical lines
LowBudget <- 150
HighBudget <-300
InefficientXint1 <- LowBudget
InefficientYint1 <- RandData$MeanSp[max(which(RandData$MeanCosts/50 <= InefficientXint1))]
InefficientXint2 <- HighBudget
InefficientYint2 <- RandData$MeanSp[max(which(RandData$MeanCosts/50 <= InefficientXint2))]
EfficientXint1 <- LowBudget
EfficientYint1 <- Efficient$TotSp[max(which(Efficient$TotCo/50 <= EfficientXint1))] #Efficient$TotSp[min(which(Efficient$TotCo/50 >= EfficientXint1))]
EfficientXint2 <- HighBudget
EfficientYint2 <- Efficient$TotSp[max(which(Efficient$TotCo/50 <= EfficientXint2))]
# Set up plotting boundaries
# to print to file, uncomment jpeg function (line 110) and dev.off (line 148)
#jpeg("./Manuscripts/Science Policy/Second revision/Figure 1/FormattedFig1_draft.jpeg")
ggplot()+
labs(x="Annual Budget over 50 years (millions of $)", y = "Expected Number of Species Recovered")+
theme(axis.title = element_text(face ="bold", size = 12))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
scale_x_continuous(expand = c(0, 0), limits = c(0,1800),breaks = pretty(Efficient$TotCo/50, n=5)) +
scale_y_continuous(expand = c(0, 0), limits = c(0,1500),breaks = pretty(Efficient$TotSp, n=5))+
## add data to plot
geom_line(data=Efficient,aes(y=TotSp, x= TotCo/50,colour = "Optimal Allocation"),size = 1) +
geom_point(data=Efficient,aes(y=TotSp, x= TotCo/50), color = "darkred", size = 2) +
geom_line(data = RandData,aes(y=MeanSpp,x=MeanCosts/50,colour = "Inefficient Allocation"),size =1)+
geom_ribbon(data = RandData, aes(y=MeanSpp,x=MeanCosts/50,
ymin = Percentile5, ymax = Percentile95),color = "white", alpha = 0.1)+
## add vertical lines
geom_vline(xintercept = LowBudget, linetype= "dashed", size = 1)+
geom_vline(xintercept = HighBudget, linetype = "dotted", size = 1)+
## put legend in better location and change colors
theme(legend.justification=c(1,0),legend.position = c(0.95,0.25),legend.title=element_blank(),
legend.key = element_rect(colour = NA, fill = NA))+
scale_colour_manual(values=c("cyan4","coral1"))+
## add arrows and letters
annotate("segment", x = InefficientXint1, xend = InefficientXint1,
y = InefficientYint1, yend = EfficientYint1,
colour = "black", size=2, alpha=0.4, arrow=arrow())+
annotate("segment", x = EfficientXint1, xend = EfficientXint2,
y = EfficientYint1, yend = EfficientYint2,
colour = "black", size=2, alpha=0.4, arrow=arrow())+
annotate("text", x = c(LowBudget +50 , HighBudget -50), y = c(600,1100), label = c("A", "B") , color="black", size=4 , fontface="bold")
#dev.off()
### save as .eps (vector format) for Galley proofs
ggsave("Fig1.pdf", device = "pdf", width = 20, height = 10, units = "cm")
#### print out values for different intercepts and differences
# species saved with low budget and inefficient spending
LowBudget
InefficientYint1
# species saved with high budget and inefficient spending
HighBudget
InefficientYint2
# species saved with low budget and efficient spending
EfficientYint1
# species saved with high budget and efficient spending
EfficientYint2
# difference between spending A and B efficient
EfficientYint2 - EfficientYint1
# difference between spending A and B inefficient
InefficientYint2 - InefficientYint1
# difference between efficient and inefficient low spending
EfficientYint1 -InefficientYint1
# difference between efficient and inefficient high spending
EfficientYint2 - InefficientYint2
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment