R/drc_plot.R

Defines functions drc_plot

#' Make a ggDRC plot
#'
#' for more info, see \href{../doc/ggDRC.html}{\code{vignette("ggDRC", package = "ggDRC")}}
#'
#' @param df2         results from drc_fit
#' @param type        type of plot
#' @param bars        error bars on/off
#' @param means       means on/off
#' @param show_n      number of n  on/off
#' @param obs         observations on/off
#' @param obs.params
#' @param vline       vertical line indicating the IC50value on/off
#' @param xlimits     xlimits
#' @param xlabels     xlabels
#' @param ...         arguments are forwarded to geoms
#'

#'
#' @return a ggPlot obbject
#' @export
#'
#' @examples
drc_plot <- function(df2,
                     type=c( "all", "average", "bars", "none", "obs", "confidence"),
                     bars=type=="bars",
                     means=type=="bars" || type=="average",
                     obs=type=="all",
                     point.size=2,
                     line.size=1,
                     bar.size=line.size,
                     show_n=F,
                     show_IC50=F,
                     vline=F,
                     xlimits=c(.0001,100),
                     xlabels=scales::format_format(drop0trailing=TRUE, scientific = FALSE),
                     legend.labels=NULL,
                     ...
){
  require(ggplot2)
  # plot raw data, model and ED50 line
  observations<-function(x) data.frame(y=mean(x), n=length(x))

  IC50values= df2 %>% tidyr::unnest(coefs) %>% filter(names == "IC50:(Intercept)")

  p<- df2 %>%
    tidyr::unnest(data)   %>% group_by(concentration) %>% dplyr::mutate (nn=n()) %>%
    ggplot(aes(concentration, response, color = compound)) +
    scale_x_log10(limits=xlimits, labels = xlabels, minor_breaks=10^(-4:3)%o%(2:10))

  if(show_IC50)
    p <- p+ scale_colour_discrete( labels=drc_labels(df2))
  if(!is.null(legend.labels))
    p <- p+ scale_colour_discrete( labels=legend.labels)

  if(obs)
    p <- p + geom_jitter( aes(concentration, response), size=point.size, width=0 )

  p <- p +
    geom_line(aes(x, pred, color = compound), data =	df2 %>% tidyr::unnest(pred), na.rm=TRUE, size=line.size, ...)

  if(means)
    p <- p + geom_point( stat="summary", fun.data=mean_se, size=point.size,...)

  if(bars)
    p <- p + geom_errorbar( stat = "summary", fun.data=mean_se, width=0.15, size=bar.size)



  if(show_n)
    p <- p + geom_text(aes(concentration, response, label=paste0("(",stat(n), ")"), color = compound), show.legend=F, stat="summary", fun.data=observations, nudge_x = .15, nudge_y = .02, size=3)

  if(vline)
    p <- p + geom_segment(aes( x=x, y=0.5, xend=x, yend=-Inf, color = compound),	linetype = 5, data = df2 %>% tidyr::unnest(coefs) %>% filter(names == "IC50:(Intercept)"))

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