Plotting dose response curves with ggplot2 and drc

前端 未结 2 974
渐次进展
渐次进展 2020-12-30 12:54

In biology we often want to plot dose response curves. The R package \'drc\' is really useful and base graphics can easily handle \'drm models\'. However, I would like to ad

2条回答
  •  难免孤独
    2020-12-30 13:51

    I am going to answer my own question and hopefully this will help others facing the same problem.

    It is of course possible to plot dose response curves with ggplot2 and the drc package with the simple addition of either geom_ or stat_smooth (method=drm, fct=LL.4(),se=FALSE) if plotting on a linear scale or geom_ or stat_smooth (method=drm, fct=L.4(),se=FALSE) if scale_x_log10() is added.

    In order to be able to use a log10 scale I've transformed my data to:

    demo <- demo %>% 
          mutate(X = 
           ifelse(X == 0, 
                  yes = (sort(demo$X[which.min(sort(demo$X)) + 1]/100)),
                  no = X
                  )
                )         #looks for the pre-lowest value in X and divides it by 100
    

    In this case, I've replaced the X = 0 value by X = 1/100th of the pre-last X-value (in this case 1e-10). You can, however, easily drop the 0 value that messes up the logarithmic plotting by omitting it from the dataset entirely, like Prism does. One thing to note, as I've found out, is that ggplot scales the axes first and then adds the data, which is why the code breaks as it tries to log10(0).

    The other subtlety is that the stat_smooth function is perfectly capable of handling drm models using method = drm but it doesn't know how to fit the 'SE' confidence intervals. Selecting se = FALSE thus enables plotting and in my humble opinion makes for a less messy plot anyway - just add the error bars.

    And finally, changing the fct = LL.4() to fct = L.4() allows plotting on a log10 scale, because again the scale is selected first and the fit is done after. So, even though the axis values are non-logarithmic, ggplot has actually transformed the dataset into log10, therefore the fitting function now needs to be just logit-4P (i.e. L.4()) instead of log-logit-4P (LL.4()).

    The geom_smooth() and stat_smooth() functions will naturally adopt the same colour as the dataset, eliminating the need to adjust the colour of the fitted function to correspond with the colour of the data points.

    In summary:

    demo <- demo %>% 
          mutate(X = 
           ifelse(X == 0, 
                  yes = (sort(demo$X[which.min(sort(demo$X)) + 1]/100)),
                  no = X
                  )
                )
    demo.long <- reshape2::melt(demo,id.vars = "X") #reshapes the demo dataset to long format
    ggplot(data = demo.long,
           aes(x = X, y = value, col = variable)
          ) + 
       geom_point() + 
       geom_smooth(method = drm, fct = L.4(), se = FALSE) +
       scale_x_log10() #plots out the dataset with the corresponding 4-parameter log-logit dose response curves
    

提交回复
热议问题