dan - 10 months ago 88

R Question

I have dose response data:

`df <- data.frame(dose=c(10,0.625,2.5,0.15625,0.0390625,0.0024414,0.00976562,0.00061034,10,0.625,2.5,0.15625,0.0390625,0.0024414,0.00976562,0.00061034,10,0.625,2.5,0.15625,0.0390625,0.0024414,0.00976562,0.00061034),viability=c(6.117463479317,105.176885855348,57.9126197628863,81.9068445005286,86.484379347143,98.3093580807309,96.4351897372596,81.831197750164,27.3331232120347,85.2221817678203,80.7904933803092,91.9801454635583,82.4963735273569,110.440066995265,90.1705406346481,76.6265869905362,11.8651732228561,88.9673125759484,35.4484427232156,78.9756635057238,95.836828982968,117.339025930735,82.0786828300557,95.0717213053837),stringsAsFactors=F)`

I fit log-logistic model to these data using the

`drc`

`R`

`library(drc)`

fit <- drm(viability~dose,data=df,fct=LL.4(names=c("Slope","Lower Limit","Upper Limit","ED50")))

I then plot this curve with the standard error using:

`pred.df <- expand.grid(dose=exp(seq(log(max(df$dose)),log(min(df$dose)),length=100)))`

pred <- predict(fit,newdata=pred.df,interval="confidence")

pred.df$viability <- pred[,1]

pred.df$viability.low <- pred[,2]

pred.df$viability.high <- pred[,3]

library(ggplot2)

p <- ggplot(df,aes(x=dose,y=viability))+geom_point()+geom_ribbon(data=pred.df,aes(x=dose,y=viability,ymin=viability.low,ymax=viability.high),alpha=0.2)+labs(y="viability")+

geom_line(data=pred.df,aes(x=dose,y=viability))+coord_trans(x="log")+theme_bw()+scale_x_continuous(name="dose",breaks=sort(unique(df$dose)),labels=format(signif(sort(unique(df$dose)),3),scientific=T))+ggtitle(label="all doses")

Finally I'd like to add the parameter estimates to the plot as a table. I'm trying:

`params.df <- cbind(data.frame(param=gsub(":\\(Intercept\\)","",rownames(summary(fit)$coefficient)),stringsAsFactors=F),data.frame(summary(fit)$coefficient))`

rownames(params.df) <- NULL

ann.df <- data.frame(param=gsub(" Limit","",params.df$param),value=signif(params.df[,2],3),stringsAsFactors=F)

rownames(ann.df) <- NULL

xmin <- sort(unique(df$dose))[1]

xmax <- sort(unique(df$dose))[3]

ymin <- df$viability[which(df$dose==xmin)][1]

ymax <- max(pred.df$viability.high)

p <- p+annotation_custom(tableGrob(ann.df),xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax)

But getting the error:

`Error: annotation_custom only works with Cartesian coordinates`

Any idea?

Also,

once plotted, is there a way to suppress row names?

Answer Source

I'm not sure if there's a way around the error within "base" `ggplot2`

. However, you can use `draw_grob`

from the `cowplot`

package to add the table grob (as described here).

Note that in `draw_grob`

the x-y coordinates give the location of the lower left corner of the table grob (where the coordinates of the width and height of the "canvas" go from 0 to 1):

```
library(gridExtra)
library(cowplot)
ggdraw(p) + draw_grob(tableGrob(ann.df, rows=NULL), x=0.1, y=0.1, width=0.3, height=0.4)
```

Another option is to resort to `grid`

functions. We create a viewport within the plot `p`

draw the table grob within that viewport.

```
library(gridExtra)
library(grid)
```

Draw the plot `p`

that you've already created:

```
p
```

Create a viewport within plot `p`

and draw the table grob. In this case, the x-y coordinates give the location of the center of the viewport and therefore the center of the table grob:

```
vp = viewport(x=0.3, y=0.3, width=0.3, height=0.4)
pushViewport(vp)
grid.draw(tableGrob(ann.df, rows=NULL))
```