带有注释的ggplot()之后的ggsave()

时间:2016-01-24 19:28:19

标签: annotations

我正在尝试将使用ggplot制作的数字与使用ggsave的grid.text制作的注释一起保存到tiff中。但是,ggsave不包含注释。我尝试使用grid.arrange和arrangeGrob(如之前的帖子中所建议的那样),但它说两个函数都找不到。似乎已经对包裹做了一些更改,因为这些答案已经发布了?

{r} AggregateBW2 = summarySE(AggregateBW1, measurevar="BW", groupvars=c("Cond","Iso","within")) figure = ggplot(AggregateBW2, aes(x=Cond, y=BW, group=within)) + geom_bar(position=position_dodge(), stat="identity", fill="#999999", colour="black") + facet_grid(Iso ~ .) + geom_errorbar(aes(ymin=BW-se, ymax=BW+se), width=.2, position=position_dodge(.9)) + scale_x_discrete(name="Surgical condition") + scale_y_continuous(name="Body Weight (g)") + theme(axis.text.x=element_text(size=18), axis.text.y=element_text(size=18), axis.title.x=element_text(size=22), axis.title.y=element_text(size=22), strip.text.y=element_text(size=18)) #+ grid.text(unit(.985,"npc"),0.5,label = "Isoflurane Percentage", rot = 270, gp=gpar(fontsize=22), check=TRUE) paperfigure = grid.arrange(figure, ncol = 1, bottom = "footnote") ggsave(file="newbodyweight-final.tiff", paperfigure)     {{1}}

1 个答案:

答案 0 :(得分:1)

而不是grid.text(),请尝试annotate()。那是因为grid.text()添加了一个实际上没有保存在情节结构中的外部注释(https://stackoverflow.com/a/12115836)。

在一个非常简单的示例中,下面的代码创建了this image

figure=ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length))+
  geom_point()+
  facet_grid(Species~.)+
  annotate(geom="text", label = "Isoflurane Percentage", y=6, x=3)

ggsave(file="newbodyweight-final.png", figure)