如何在ggplot2输出中添加表格?

时间:2013-05-21 21:48:29

标签: r ggplot2

有没有快速的方法可以将表格添加到我的ggplot2图表中?我希望此表具有scale_x_continuous()中指定的相同断点处的每一行的值,但旁边的百分比(%)符号。我的最终目标是创建如下图所示的内容。但是,我不知道如何添加表格。

gains chart example

以下代码块只在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), colour = 'red') +
           geom_line(aes(y = b), colour = 'blue') + scale_x_continuous(breaks = seq(0,100,10))
df.plot

类似的问题被问到here,但给出的答案更多的是一种解决方法,对于有2行的表格看起来不太好。我将要弄乱Brian Diggs提供的线索,但我想我会发布这个以防任何人已经做过这样的事情。任何帮助将不胜感激!

编辑:感谢@baptiste帮助我解决这个问题。我在下面发布了自己的回复,完成了他的开始。

2 个答案:

答案 0 :(得分:9)

以下是学习者使用的策略的基本示例:

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)

enter image description here

答案 1 :(得分:6)

这是一个脚本,用于创建我要设置的常规表。请注意,我通过更改每行scale_y_continuous下的名称来包含表格标题。

require(ggplot2)
require(gridExtra)
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
lab.df <- data.frame(lab1 = letters[11:20],
                     lab2 = letters[1:10])

df.table1 <- ggplot(lab.df, aes(x = lab1, y = 0,
                                 label = lab1)) +
  geom_text(size = 5, colour = "red") + 
  theme_minimal() + 
  scale_y_continuous(breaks=NULL, name = "Model Lift") +
  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_text(angle = 0, hjust = 5))

df.table2 <- ggplot(lab.df, aes(x = lab2, y = 0,
                                 label = lab2)) +
  geom_text(size = 5, colour = "blue") + 
  theme_minimal() + 
  scale_y_continuous(breaks=NULL, name = "Random")+
  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_text(angle = 0, hjust = 3.84))


# silly business to align the two plot panels    
gA <- ggplotGrob(df.plot)
gB <- ggplotGrob(df.table1)
gC <- ggplotGrob(df.table2)

maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3], gC$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)
gC$widths[2:3] <- as.list(maxWidth)

grid.arrange(gA, gB, gC, ncol=1, heights=c(10, .3, .3))

sample plot

相关问题