将数据框绘制为表格 [英] Plot a data frame as a table

查看:118
本文介绍了将数据框绘制为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在远离Word/Excel表,并尝试在 R 中生成一个表.我有一个数据框,我想简单地将其打印为图表,同时能够对单元格进行着色/着色,并且通常具有美观性.

I am moving away from Word/Excel tables and trying to generate a table in R. I have a data frame that I'd like to simply print as a plot, while being able to shade/colour cells and generally play with the aesthetics.

x <- data.frame(row.names=paste("Name",1:10))
x[,1] <- 1:10
x[,2] <- sample(1:100,10)
x[,3] <- sample(LETTERS[1:26],10)
colnames(x) <- c("Value 1", "Value 2", "Label")

View(x)提供了我想要的表格的确切格式,就像可保存的绘图一样.

View(x) provides the exact format I'd like my table, just as a save-able plot.

我尝试了

plot(x,type="h")

但是收到一个错误:

plot.default(...)中的错误:形式参数类型"与 多个实际参数

Error in plot.default(...) : formal argument "type" matched by multiple actual arguments

我已经看到了如何输出两列的表格,但是如何按原样绘制数据框?奖励点,用于显示如何将该表格粘贴到我创建的另一个散点图下方,以便输出ggsave具有散点图及其下的表格.

I have seen how to output tables with two columns, but how can I plot the data frame as-is? Bonus points for showing how to stick that table below another scatter plot that I have created, so that the output ggsave has the scatter plot with the table under it.

推荐答案

由于我要争取加分:

   #Plot your table with table Grob in the library(gridExtra)
   ss <- tableGrob(x)

   #Make a scatterplot of your data
   k <- ggplot(x,aes(x=x$"Value 1",y=x$"Value 2")) + 
   geom_point()

   #Arrange them as you want with grid.arrange
   grid.arrange(k,ss)

如果需要,您可以更改行数,列数,高度等.

You can change the number of rows, columns, height and so on if you need to.

祝你好运

http://cran.r-project.org/web/packages/gridExtra/gridExtra .pdf

这篇关于将数据框绘制为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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