10

Sample data:

### Data
df <- data.frame(year = seq(as.Date("1998/1/1"), as.Date("2012/1/1"), "years"),
                 ton = sample(200:500, 15, replace = TRUE),
                 trend = sample(50:100, 15, replace = TRUE),
                 count = sample(100:200, 15, replace = TRUE))

### Load the needed libraries
library(ggplot2)
library(scales)
library(grid)
library(gridExtra)

### Make a plot
plot1 <- ggplot(data = df, aes(x = year, y = ton)) + geom_bar(stat = "identity") +
  scale_x_date(breaks = df$year, labels = date_format("%Y")) +
  theme_bw()

plot2 <- ggplot(data = df, aes(x = year, y = trend)) + geom_line() +
  geom_point(shape = 21, size = 4, fill = "white") +
  scale_x_date(breaks = df$year, labels = date_format("%Y")) +
  theme_bw()

I combined both plots using the grid.arrange, and here is the syntax:

grid.arrange(plot1, plot2, nrow = 2, top = "Sample data trend")

I tried the following code to make a table (the year and count variables, but the result is not good.

### Create new dataframe for the table
df1 <- data.frame(count = df$count,
                  row.names = df$year)
df1 <- as.data.frame(t(df1))

### Add table below the graph
tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)))
tbl <- tableGrob(df1, rows=NULL, theme=tt)
grid.arrange(plot1, plot2, tbl,
             nrow = 3,
             as.table = TRUE)

The result of the above syntax: enter image description here

Question: How can I make it fit in the graph area, and how to make the table closer to the line plot graph so that the white space will be minimized? And if there's any way to customize it?

Thank you.

2
  • grid.arrange got heights and widths parameters. Use them. Commented Dec 15, 2016 at 13:14
  • I tried the following syntax: grid.arrange(plot1, plot2, tbl, nrow = 3, heights = c(15, 15, 5), widths = c(10, 10, 5), as.table = TRUE), but the result is still the same, the table overlaps across the graph area. Commented Dec 15, 2016 at 13:20

1 Answer 1

8
tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)),
                     base_size = 10,
                     padding = unit(c(2, 4), "mm"))
tbl <- tableGrob(df1, rows=NULL, theme=tt)

png("E:/temp/test.png", width = 1000, height = 1000)
grid.arrange(plot1, plot2, tbl, 
             nrow = 3, heights = c(2, 2, 0.5))
dev.off()

resulting plot

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

4 Comments

I tried your syntax, but the resulting table is small. Is there a way to make it big? How can I remove the months and days, retaining only the year, in the table?
Adjust the parameters until you are happy. The main point is that the size of the device needs to be big enough or the table width small enough that the table fits into the device. formating dates is a different question and a quick search (or reading the documentation) should help you there.
Thank you very much. In my device, the syntax is: ppi <- 300 png(width = ppi*9, height = ppi*6, res = 300) grid.arrange(plot1, plot2, tbl, nrow = 3, heights = c(2, 2, 0.5)) dev.off() My last question, is it possible to add a title for the table? Or something like this: "Number of count per year", at the top or bottom of the table?
Everything is possible. It would probably be easiest to combine the table with a textGrob using arrangeGrob.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.