Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中制作一个结构类似于平面网格的表格_R - Fatal编程技术网

在R中制作一个结构类似于平面网格的表格

在R中制作一个结构类似于平面网格的表格,r,R,我想在R中创建一个“嵌套”类型的表,它反映了我可以使用facet_wrap使用ggplot创建的绘图的格式 以下是一些数据和代码: tabledata = structure(list(row = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,1L, 2L, 1L, 2L, 1L, 2L), col = c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1

我想在R中创建一个“嵌套”类型的表,它反映了我可以使用facet_wrap使用ggplot创建的绘图的格式

以下是一些数据和代码:

tabledata = structure(list(row = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,1L, 2L, 1L, 2L, 1L, 2L), 
                       col = c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), 
                       grp1 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), 
                                        .Label = c("a", "b"), class = "factor"), 
                       grp2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
                                        .Label = c("g", "h"), class = "factor"), 
                       value = c(9L, 9L, 14L, 8L, 10L, 9L, 8L, 15L, 2L, 1L, 3L, 4L, 1L, 5L, 2L, 4L)), 
                  .Names = c("row", "col", "grp1", "grp2", "value"), class = "data.frame", 
                  row.names = c(NA, -16L))

ggplot(tabledata, aes(grp2, value, shape = grp1)) + geom_jitter() + facet_grid(row ~ col)
产生这幅图的是:

下面是我想制作的表(使用透视表可以轻松完成,但显然这并不理想):


可以使用
tables
包中的
tabular()
函数,使用以下代码创建嵌套表格

tabular(
    (Heading()*Factor(row)*Heading()*grp1)~
    (Heading()*Factor(col)*Heading()*grp2)*Heading()*value*Heading()*identity, 
data = tabledata)

然后,可以使用
write.csv.tabular()
将表格保存为.csv文件

Tidyverse刚刚添加了一个内置嵌套格式的表包。它被称为“gt”(大桌子)