将表添加到ggplot图中

时间:2016-10-31 02:31:08

标签: r ggplot2

我有剂量反应数据:

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)

我使用drc R包将log-logistic模型与这些数据相符合:

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

然后我使用以下标准错误绘制此曲线:

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")

最后,我想将参数估计值作为表格添加到图表中。我正在尝试:

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)

但得到错误:

Error: annotation_custom only works with Cartesian coordinates 

有什么想法吗?

另外, 一旦绘制,有没有办法抑制行名?

1 个答案:

答案 0 :(得分:5)

我不确定“base”annotation_custom中的ggplot2错误是否有办法解决。但是,您可以使用draw_grob包中的cowplot添加表格grob(如here所述)。

请注意,在draw_grob中,xy坐标给出了表格grob左下角的位置(“画布”的宽度和高度坐标从0到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)

enter image description here

另一种选择是采用grid函数。我们在图中创建一个视口p在该视口中绘制表格grob。

library(gridExtra)
library(grid)

绘制您已创建的情节p

p

在plot p中创建一个视口并绘制表格grob。在这种情况下,x-y坐标给出了视口中心的位置,因此得到了表格grob的中心:

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

enter image description here

UPDATE:要删除表格grob的背景颜色,可以操作表格grob主题元素。请参阅下面的示例。我也证明了这些数字是正确的,所以它们在小数点上排成一行。有关编辑表格凹凸的更多信息,请参阅tableGrob vignette

thm <- ttheme_minimal(
  core=list(fg_params = list(hjust=rep(c(0, 1), each=4), 
                             x=rep(c(0.15, 0.85), each=4)),
            bg_params = list(fill = NA)),
  colhead=list(bg_params=list(fill = NA)))

ggdraw(p) + draw_grob(tableGrob(ann.df, rows=NULL, theme=thm), 
                      x=0.1, y=0.1, width=0.3, height=0.4)

enter image description here