Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a table to my ggplot2 output?

Tags:

r

ggplot2

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.

like image 251
zap2008 Avatar asked May 21 '13 21:05

zap2008


People also ask

Can you make tables in ggplot2?

This R package uses ggplot2 syntax to create great tables. for plotting. The grammar of graphics allows us to add elements to plots. Tables seem to be forgotten in terms of an intuitive grammar with tidy data philosophy – Until now.

Do you need a Dataframe for Ggplot?

ggplot only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this “long” form with the melt function in the library reshape2 .

What is a Ggplot Grob?

First off a grob is just short for “grid graphical object” from the low-level graphics package grid; Think of it as a set of instructions for create a graphical object (i.e. a plot). The graphics library underneath all of ggplot2's graphical elements are really composed of grob's because ggplot2 uses grid underneath.

Which ggplot2 function is called to add a title to a chart?

You add a chart title with the ggtitle() function.


1 Answers

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

like image 163
baptiste Avatar answered Sep 28 '22 10:09

baptiste