Inserting a table under the legend in a ggplot2 and saving everything to a file

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

I have the following data.frame which I would like to plot with ggplot2:

df = data.frame(mean=c(1.96535,2.133604,1.99303,1.865004,2.181713,1.909511,2.047971,1.676599,2.143763,1.939875,1.816028,1.95465,2.153445,1.802517,2.141799,1.722428), sd=c(0.0595173,0.03884202,0.0570006,0.04934336,0.04008221,0.05108064,0.0463556,0.06272475,0.04321496,0.05283728,0.05894342,0.05160038,0.04679423,0.05305525,0.04626291,0.0573123), par=as.factor(c("p","p","m","m","p","p","m","m","m","m","p","p","m","m","p","p")), group=as.factor(c("iF","iF","iF","iF","iM","iM","iM","iM","RF","RF","RF","RF","RM","RM","RM","RM")), rep=c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2)) 

This is my code for doing that:

p 

which produces this figure:

I would like to add this data.frame under the legend:

leg.df = data.frame(statistic = c("pp(par)","pp(g)","pp(s)","fc(p/m)"), value = c(0.96,0.94,0.78,1.5)) 

I followed this stackoverflow thread for inserting a table under the legend in a ggplot2 using this code:

g_legend 

which produces exactly what I want:

The only problem is that I cannot get it to print everything to a file. I tried specifying pdf() in front of the entire block of code and dev.off() after it, but that doesn't seem to work. So I can only get it to print to the R screen.

So I guess I should be handling the legend part differently but I have no idea how.

Any suggestions?

回答1:

Use the second solution in the link with arranegGrob:

leg.df.grob 

Then you can save using ggsave:

ggsave('plot.png',pp) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!