14

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.

gains chart example

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.

2
  • 1
    I don't have time to really work on this, but one approach might be to use grid.arrange and grid.table from the gridExtra package. I suspect it will be hard to line things up nicely that way, though, without some manual tinkering. Commented May 21, 2013 at 22:29
  • 1
    I think the most straight-forward way is to create the table using ggplot2 itself. This was nicely illustrated with this example (may need updating some of the code as it's relatively old). Commented May 21, 2013 at 22:55

2 Answers 2

10

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)

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

This looks like the correct solution. I'm gonna work through this and see if I can build the gains plot I showed you in my picture.
there are two steps in this procedure, which can be done independently to make it easier; i) first, get the "table" plot on its own, even with axes etc if that helps; ii) align the two graphs in one page.
What exactly does the ggplotGrob() function do? the documentation is kinda crappy and I don't really know what I'm looking at in the output.
it's building the ggplot and converting it to a gtable, which is the object that will eventually get plotted. As such it contains the layout information (viewport widths, heights, etc.) that you need to align two plots. Try names(gt) to get a sense of what the gtable contains.
i included my own response that builds off of yours
|
8

Here is a script that creates the general table that I set out to make. Notice that I included table titles by changing the names under scale_y_continuous for each row.

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

2 Comments

nicely done. There's a bit of a mismatch on the left side, if that's a problem I'm sure it can be fixed byt looking more closely at the plot margins and widths.
it also might have something to do with the position of the titles "Model Lift" and "Random." I bet if you messed around with the hjust option a bit it would go away. The other plot I made didn't have those issues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.