我想做一个"嵌套" 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)
这是我想要制作的表格(可以使用数据透视表轻松完成,但显然这并不理想):
答案 0 :(得分:0)
可以使用以下代码使用tabular()
包中的tables
函数创建嵌套表。
tabular(
(Heading()*Factor(row)*Heading()*grp1)~
(Heading()*Factor(col)*Heading()*grp2)*Heading()*value*Heading()*identity,
data = tabledata)
然后可以使用write.csv.tabular()
将表保存为.csv文件。
答案 1 :(得分:-1)
Tidyverse刚刚添加了一个内置嵌套格式的表包。它被称为“ gt”(大表)https://blog.rstudio.com/2020/04/08/great-looking-tables-gt-0-2/