我如何添加一个表格到我的ggplot2输出? [英] How can I add a table to my ggplot2 output?
问题描述
有没有一种快捷方式将表添加到我的ggplot2图中?我希望此表的每行的值与 scale_x_continuous()
中指定的相同断点处的值相同,但其旁边有百分号(%)符号。我的最终目标是创建如下图所示的内容。不过,我不知道如何添加表格。
下面的代码块在ggplot2中只有两行,应该足以为我提供一个示例:
require(ggplot2)
df < - data.frame(a = seq(0,90,10),b = seq(10 ,100,10))
df.plot < - ggplot(data = df,aes(x = seq(1,100,10)))+ geom_line(aes(y = a),color ='red ')+
geom_line(aes(y = b),color ='blue')+ scale_x_continuous(breaks = seq(0,100,10))
df.plot
类似的问题被问到 here ,但给定的答案更多的是解决方法,对于有两行的表格看起来不太好。我会乱搞Brian Diggs提供的线索,但我想我会发布这个以防万一任何人已经做了这样的事情。任何帮助将不胜感激!
编辑:感谢@baptiste帮助我弄清楚了这一点。我发布了自己的回复,下面是他完成的任务。
以下是学员使用的策略的一个基本示例:(ggplot2)
df < - data.frame(a = seq(0,90,10),b(b) = seq(10,100,10))
df.plot< - ggplot(data = df,aes(x = seq(1,100,10)))+
geom_line(aes(y = a),color ='red')+
geom_line(aes(y = b),color ='blue')+
scale_x_continuous(breaks = seq(0,100,10))
#制作表格内容的虚拟标签
df $ lab < - month.abb [ceiling((df $ a + 1)/ 10)]
df.table < - ggplot(df,aes(x = a,y = 0,
label = lab,color = b))+
geom_text(size = 3.5)+
theme_minimal()+
scale_y_continuous(breaks = NULL)+
theme(panel.grid.major = element_blank(),legend.position =none,
panel.border = element_blank(),axis.text .x = element_blank(),
axis.ticks = ele (),
axis.title.x = element_blank(),
axis.title.y = element_blank())
gA< - ggplotGrob(df.plot)
gB < - ggplotGrob(df.table)[6,]
gB $ heights < - unit(1,line)
require gridExtra)
gAB< - rbind(gA,gB)
grid.newpage()
grid.draw(gAB)
Is there a quick way to add a table to my ggplot2 graph? I would like this table to have the value of each line at the same breakpoints as specified in scale_x_continuous()
, but with the percentage (%) symbol next to them. My end goal is to create something like the image below. However, I don't know how to add the table.
The following block of code just makes two lines in ggplot2 and should be adequate to provide me with an example:
require(ggplot2)
df <- data.frame(a = seq(0, 90, 10), b = seq(10, 100, 10))
df.plot <- ggplot(data = df, aes(x = seq(1, 100, 10))) + geom_line(aes(y = a), colour = 'red') +
geom_line(aes(y = b), colour = 'blue') + scale_x_continuous(breaks = seq(0,100,10))
df.plot
A similar question was asked here, but the given answer is more of a workaround and wouldn't look good for a table with 2 rows. I am going to mess around with the clues provided by Brian Diggs, but I figured I would post this in case anyone has already done something like this. Any help would be greatly appreciated!
Edit: Thanks to @baptiste for helping me figure this out. I posted my own response below that finished what he started.
Here's a basic example of the strategy used by learnr:
require(ggplot2)
df <- data.frame(a = seq(0, 90, 10), b = seq(10, 100, 10))
df.plot <- ggplot(data = df, aes(x = seq(1, 100, 10))) +
geom_line(aes(y = a), colour = 'red') +
geom_line(aes(y = b), colour = 'blue') +
scale_x_continuous(breaks = seq(0,100,10))
# make dummy labels for the table content
df$lab <- month.abb[ceiling((df$a+1)/10)]
df.table <- ggplot(df, aes(x = a, y = 0,
label = lab, colour = b)) +
geom_text(size = 3.5) +
theme_minimal() +
scale_y_continuous(breaks=NULL)+
theme(panel.grid.major = element_blank(), legend.position = "none",
panel.border = element_blank(), axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
gA <- ggplotGrob(df.plot)
gB <- ggplotGrob(df.table)[6,]
gB$heights <- unit(1,"line")
require(gridExtra)
gAB <- rbind(gA, gB)
grid.newpage()
grid.draw(gAB)
这篇关于我如何添加一个表格到我的ggplot2输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!