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

zap*_*008 10 r ggplot2

有没有快速的方法将表添加到我的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), colour = 'red') +
           geom_line(aes(y = b), colour = 'blue') + scale_x_continuous(breaks = seq(0,100,10))
df.plot
Run Code Online (Sandbox Code Playgroud)

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

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

bap*_*ste 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


zap*_*008 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))
Run Code Online (Sandbox Code Playgroud)

样本图