Juan Pablo Edwards Molina
pkg <- c("ggplot2", "gridExtra","reshape2", "plyr", "knitr")
sapply(pkg, require, character.only=TRUE)
## ggplot2 gridExtra reshape2 plyr knitr
## TRUE TRUE TRUE TRUE TRUE
Plots ce50 frequency
dat <- data.frame(CE50 = c(rnorm(20, 5,0.5), rnorm(20, 3,0.5), rnorm(20, 8,0.5)),
Fungicida = rep(c("Triazol", "Estrobilurina", "Carboxamida"), e = 20))
dat[c(1:3,21:24, 41:44),]
## CE50 Fungicida
## 1 4.334130 Triazol
## 2 4.769150 Triazol
## 3 5.496956 Triazol
## 21 3.481167 Estrobilurina
## 22 2.788952 Estrobilurina
## 23 2.784176 Estrobilurina
## 24 3.413175 Estrobilurina
## 41 7.914746 Carboxamida
## 42 8.068364 Carboxamida
## 43 8.798729 Carboxamida
## 44 8.175325 Carboxamida
with(dat, tapply(CE50, list(Fungicida), mean))
## Carboxamida Estrobilurina Triazol
## 7.979391 3.153627 5.155823
plot_1 = ggplot(dat, aes(x = CE50, fill = Fungicida))+geom_density(alpha = 0.5, size=0.2) +
scale_x_continuous(breaks=seq(0, 12, 1))
plot_2 =ggplot(dat,aes(x=Fungicida, y=CE50, fill=Fungicida)) +
geom_boxplot() + guides(fill=FALSE) + coord_flip() +
scale_y_continuous(breaks=seq(0, 12, 1))
Set the desired legend properties before extraction to grob
plot_1 = plot_1 + theme(legend.key = element_blank())
Extract the legend from one of the plots
getLegend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
leg = getLegend(plot_1)
Remove legend from other plots
plot_1 = plot_1 + theme(legend.position = 'none')
plot_2 = plot_2 + theme(legend.position = 'none')
Remove the y-axis
plot_1 = plot_1 + theme( axis.text = element_blank())
plot_2 = plot_2 + theme( axis.text.y = element_blank())
Remove the margin from the plots and set the XLab to null
tmp = theme(panel.margin = unit(c(0, 0, 0, 0), units = 'cm'),
plot.margin = unit(c(0, 0, 0, 0), units = 'cm'))
plot_1 = plot_1 + tmp + labs(x = NULL, y = NULL)
plot_2 = plot_2 + tmp + labs(x = NULL)
Add the XLabel back to the bottom plot
plot_2 = plot_2 + labs(y = "CE50")
Remove the X-Axis from all the plots but the bottom one
plot_1 = plot_1 + theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())
Store plots in a list for ease of processing
plots = list()
plots[[1]] = plot_1
plots[[2]] = plot_2
plotGrobs = lapply(plots, ggplotGrob)
plotGrobs[[1]]$widths[2:5]
## [1] 1grobwidth+0lines sum(0cm, 0.15cm+0.1cm) 1null
## [4] 0cm
maxWidth = plotGrobs[[1]]$widths[2:5]
for(i in length(plots)) {
maxWidth = grid::unit.pmax(maxWidth, plotGrobs[[i]]$widths[2:5])
}
for(i in length(plots)) {
plotGrobs[[i]]$widths[2:5] = as.list(maxWidth)
}
plotAtPos = function(x = 0.5, y = 0.5, width = 1, height = 1, obj) {
pushViewport(viewport(x = x + 0.5*width, y = y + 0.5*height, width = width, height = height))
grid.draw(obj)
upViewport()
}
grid.newpage()
plotAtPos(x = 0.05, y = 0.5, width = 0.7, height = 0.4, plotGrobs[[1]])
plotAtPos(x = 0.05, y = 0.1, width = 0.7, height = 0.4, plotGrobs[[2]])
plotAtPos(x = 0.8, y = 0, width = 0.1, height = 1, leg)
df <- data.frame(cbind(rnorm(30),rnorm(30, mean=1.1),
rnorm(30, mean=.9),rnorm(30, mean=1),
rnorm(30, mean=.2),rnorm(30, mean=.5)),
rnorm(30, mean=0.5),rnorm(30, mean=4))
colnames(df) <- c("Boscalid.Baseline","Boscalid.Today","Azoxistrobin.Baseline","Azoxistrobin.Today","Carbendazim.Baseline","Carbendazim.Today","Tebuconazole.Baseline","Tebuconazole.Today")
df_log<-log(df) # ignore the warning with NA
head(df_log)
## Boscalid.Baseline Boscalid.Today Azoxistrobin.Baseline
## 1 0.1366852 0.36348845 -0.15789817
## 2 -2.0502427 0.29458994 0.17824616
## 3 0.9221527 0.32249740 0.03138694
## 4 0.4436483 0.37800878 -0.27257574
## 5 NaN 0.01220434 -1.41059077
## 6 NaN -2.00426456 -0.07876532
## Azoxistrobin.Today Carbendazim.Baseline Carbendazim.Today
## 1 -0.1276183 0.7578196 NaN
## 2 0.7582330 NaN 0.1983790
## 3 1.1194408 NaN -0.3529981
## 4 0.1022459 -0.5513382 NaN
## 5 -0.8787811 NaN -0.8254844
## 6 -0.2968642 -1.5731308 0.4833412
## Tebuconazole.Baseline Tebuconazole.Today
## 1 0.1094057 1.455752
## 2 0.4262606 1.738725
## 3 NaN 1.387058
## 4 0.6936788 1.121963
## 5 NaN 1.155737
## 6 0.1115504 1.455291
df$id <- 1:nrow(df)
df.m<- melt(df, "id")
df.m$IA <- factor(gsub("\\..*$", "", df.m$variable))
df.m$Momento <- factor(gsub(".*\\.", "", df.m$variable))
colnames(df.m)
## [1] "id" "variable" "value" "IA" "Momento"
colnames(df.m)[3] <- "CE50"
head(df.m)
## id variable CE50 IA Momento
## 1 1 Boscalid.Baseline 1.1464672 Boscalid Baseline
## 2 2 Boscalid.Baseline 0.1287037 Boscalid Baseline
## 3 3 Boscalid.Baseline 2.5146979 Boscalid Baseline
## 4 4 Boscalid.Baseline 1.5583823 Boscalid Baseline
## 5 5 Boscalid.Baseline -1.2718473 Boscalid Baseline
## 6 6 Boscalid.Baseline -0.8989097 Boscalid Baseline
p <- ggplot(data = df.m, aes(x=CE50)) + geom_density(aes(fill=Momento), alpha = 0.4)
p <- p + facet_wrap( ~ IA)
p + scale_fill_brewer(palette = "Set1")