The "new way" to make DRC plots using tidyverse methods and a new workflow.

I have defined some functions to fit the models, make a ggplot object from the fit, and "kable" the fit results.

library(knitr)
opts_chunk$set(fig.width=6, fig.height=4, out.width='60%',
                      echo=TRUE, warning=FALSE, message=FALSE)
# drc_fit expects a data frame with columns data, conc, and cpd (in that order). 

library(ggplot2)
library(ephys2)
library(dplyr)
library(knitr)
theme_set(theme_classic())

load("firstpeak") # our testdata
head(firstpeak) %>% kable()
firstpeak %>% drc_fit() %>% drc_plot() 
firstpeak %>% drc_fit(params=3) %>% drc_kable() 
firstpeak %>% drc_fit(params=3) %>% drc_plot(show_IC50 = T)

drc_plot options

firstpeak %>% drc_fit() -> FIT  # store the fit for subesequent plots


FIT %>% drc_plot( type = "all"           ) + ggtitle("all") 
FIT %>% drc_plot( type = "average"       ) + ggtitle("average")
FIT %>% drc_plot( type = "bars"          ) + ggtitle("bars")
FIT %>% drc_plot( type = "bars", obs = T ) + ggtitle("bars + observations")
FIT %>% drc_plot( type = "none"          ) + ggtitle("none")  
FIT %>% drc_plot( type = "bars", line.size = 2, bar.size=1.5,point.size = 4) + 
  geom_point(size=3, alpha=.3, aes(pch=compound)) +
  ggtitle("bars + observations: add observations as geom_point for full control") 
FIT %>% drc_plot(type="bars", show_n=T, vline = T) 

For full design control, build the layers yourself:

library(tidyr)# for unnest
FIT %>% drc_plot(type="none", lty=5) + 
   geom_errorbar(stat="summary", width=0, lwd=2, alpha=.5) + 
   geom_point(stat="summary", pch=16, size=5, alpha=.5) + 
   geom_point(color="red",  size=.2) + 
   geom_text(stat="summary", fun.data=function(x) data.frame(y=mean(x), label=length(x)), 
            nudge_y = .05, nudge_x=.15, color="grey")+
   geom_segment(aes( x=x, y=0.5, xend=x, yend=-Inf, color = compound),  linetype = 3, 
               data = FIT %>% unnest(coefs) %>% filter(names == "IC50:(Intercept)")) 

The ggplot can then be customized the usual way:

library(cowplot)
library(NMItemplates)

FIT %>% drc_plot(type="bars", show_n=T) +
  ggtitle("DRC of ICA on LUHMES cells") + 
  ylab("First peak repsonse")+ 
  xlab("Concentration [µM]") + 
  annotation_logticks(side="b", colour = "grey")+ 
  scale_color_NMIblues(labels=drc_labels(FIT))+
  scale_y_continuous(breaks=seq(1,0,length.out = 5), labels = scales::percent)+ 
  scale_x_log10(limits=c(.0003,10), breaks=c(0.001, 0.01, 0.1,1), minor_breaks=10^(-4:3)%o%(2:10)) + 
  theme_NMI()+
  theme(legend.position = c(.7,.9), 
        legend.background = element_rect(fill="white"),
        legend.title = element_blank(), 
        panel.grid.major = element_line(color=rgb(0,0,1,.09), linetype=1),
        panel.grid.minor = element_line(color=rgb(0,0,1,.07), linetype=5)) 


tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.