在ggplot2中的图表下面添加一个数值表 [英] Adding a table of values below the graph in ggplot2

查看:968
本文介绍了在ggplot2中的图表下面添加一个数值表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例数据:

$ $ p $ ###数据
df < - data.frame(year = seq( as.Date(1998/1/1),as.Date(2012/1/1),年),
ton = sample(200:500,15,replace = TRUE),
趋势=样本(50:100,15,替换= TRUE),
计数=样本(100:200,15,替换= TRUE))

###负载所需的库
库(ggplot2)
库(缩放)
库(网格)
库(gridExtra)

###绘制一张图
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()

我使用 grid.arrange ,这里是语法:

$ $ p $ code> grid.arrange(plot1,plot2,nrow = 2,top =示例数据趋势)

我试了下面的代码来创建一个表( year) count 变量,但结果不理想。

 # ##为表
df1< - data.frame(count = df $ count,
row.names = df $ year)
df1< - as.data创建新的数据帧。 frame(t(df1))

###在图表下面添加表格
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)

以上语法的结果:


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:

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.

解决方案

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()

这篇关于在ggplot2中的图表下面添加一个数值表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆