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.900578       Triazol
## 2  3.940956       Triazol
## 3  5.490932       Triazol
## 21 3.442890 Estrobilurina
## 22 2.847595 Estrobilurina
## 23 3.177947 Estrobilurina
## 24 3.202438 Estrobilurina
## 41 8.140163   Carboxamida
## 42 8.312908   Carboxamida
## 43 8.423018   Carboxamida
## 44 6.865978   Carboxamida
with(dat, tapply(CE50, list(Fungicida), mean))
##   Carboxamida Estrobilurina       Triazol 
##      8.046923      2.954555      5.080149
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)
## [3] 1null                  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)

Plot today vs base lines

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               NaN      0.4790873                   NaN
## 2               NaN      1.2636378             0.5939390
## 3        -1.0063198      0.2517046             1.1562157
## 4               NaN      0.6149455             0.2523065
## 5               NaN     -0.1148832            -0.8197756
## 6        -0.2536008      0.4128261             0.8879396
##   Azoxistrobin.Today Carbendazim.Baseline Carbendazim.Today
## 1          0.3025670            0.3818764       -0.89244591
## 2                NaN           -0.8643732       -0.04399245
## 3         -1.6204639            0.1118824               NaN
## 4          0.6101353                  NaN       -0.17110735
## 5          0.9991246           -0.8857082       -1.01886626
## 6         -0.7902476           -1.2407810       -0.63077299
##   Tebuconazole.Baseline Tebuconazole.Today
## 1            -0.9098877           1.523889
## 2                   NaN           1.437837
## 3            -2.8552726           1.426533
## 4            -1.4140437           1.147876
## 5             0.0866569           1.353644
## 6                   NaN           1.474889
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 -0.3499053 Boscalid Baseline
## 2  2 Boscalid.Baseline -0.4136871 Boscalid Baseline
## 3  3 Boscalid.Baseline  0.3655619 Boscalid Baseline
## 4  4 Boscalid.Baseline -0.6084438 Boscalid Baseline
## 5  5 Boscalid.Baseline -1.4888456 Boscalid Baseline
## 6  6 Boscalid.Baseline  0.7760015 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")