如何在图表中添加表格? [英] How can I add a table to a graph?

查看:116
本文介绍了如何在图表中添加表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令创建了躲避的条形图:

I have created a dodged bar chart using the following commands:

a = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2) 
b = c("A","A","A","B","B","B","B","C","C","D","D","D","D","D") 
c = c(60,20,20,80,5,5,5,50,50,25,25,25,20,5) 
dat = data.frame(Group=a, Member=b, Percentage=c) 
ggplot(dat, aes(x=Member, y=Percentage)) + geom_bar(stat="identity", position="dodge", fill="white", colour="black") 

如果我有一组值:

table_values = c("2", "4", "2", "1")
table_total = c("A", "B", "C", "D")
tab = data.frame(Type=table_total, Value=table_values)

如何将其作为表格添加到图形中以使其对齐?像这样:

How can I add this as a table to my graph so that it is aligned? Something like this:

我可以手动执行此操作,但是我要生成很多图,所以我想知道是否可以自动化.有关如何执行此操作的任何建议?

I can do this manually but I have a lot of graphs to generate so I was wondering if this can be automated. Any suggestions on how to do this?

推荐答案

有点麻烦,但是您可以使用geom_text进行注释,将文本设置为稍负的y值.这会将其放置在绘图区域中,而不是在轴下方.

It's a bit of a hack, but you can annotate with geom_text, placing the text as a slightly negative y value. This puts it into the plot area rather than below the axis.

ggplot(dat, aes(x=Member, y=Percentage)) + 
  geom_bar(stat="identity", position="dodge", fill="white", colour="black") +
  geom_text(aes(x=table_total, label=table_values), y=-2, data=tab)

我更参与的方法是创建两个单独的图,一个是条形图,一个是表格"(关闭几乎所有主题元素),并使用类似align.plots的名称(不确定是否是正确的名称).

I more involved approach would be to create two separate plots, one that is the bar chart, one that is the "table" (turning off almost all the theme elements) and using something like align.plots (not sure if that is the right name) in the ggExtra package.

这篇关于如何在图表中添加表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆