【问题标题】:Remove rownames in gridExtra::tableGrob删除 gridExtra::tableGrob 中的行名
【发布时间】:2017-04-08 21:32:56
【问题描述】:

我想用 ggplot 绘制一个表格,但似乎无法丢失 row.names。

我试过了:

row.names(cov_table_a)<-NULL # outside the plot
row.names = FALSE            # inside the annotation_custom

这是我的数据:

cov_table_a<-
structure(list(Year = structure(1:16, .Label = c("1970", "1971", 
"1972", "1973", "1974", "1975", "1977", "1979", "1980", "1981", 
"1982", "1983", "1984", "1985", "1986", "1987", "1988", "1990", 
"1991", "1992", "2000", "2001", "2003", "2004", "2007", "2008", 
"2009", "2010", "2011", "2012", "2013", "2014"), class = "factor"), 
`Percentage \nof missing data` = structure(c(4L, 2L, 4L, 6L, 
1L, 12L, 5L, 21L, 21L, 11L, 10L, 13L, 9L, 17L, 18L, 14L), .Label = c("0%", 
"1%", "100%", "17%", "24%", "31%", "35%", "5%", "58%", "59%", 
"60%", "70%", "71%", "72%", "8%", "86%", "90%", "92%", "95%", 
"98%", "99%"), class = "factor")), .Names = c("Year", "Percentage \nof missing data"
), row.names = c(NA, -16L), class = "data.frame")

这是我尝试绘制它的方式:

library(ggplot2)
library(gridExtra)

# First I create an empty graph with absolutely nothing :
qplot(1:10, 1:10, geom = "blank") + theme_bw() + theme(line = element_blank(), text = element_blank()) +
# Then I add my table :
annotation_custom(grob = tableGrob(cov_table_a), xmin=-5, xmax=10,ymin=1, ymax=10)

【问题讨论】:

  • 请不要将虚拟 ggplots 用于嵌入表格的唯一目的! grid.draw(tableGrob()) 是推荐的方式。

标签: r ggplot2 gridextra rowname


【解决方案1】:

注意:如果要在空画布上绘制表格,则不需要使用 ggplot 作为容器:

grid.table(cov_table_a, rows = NULL)

grid.draw(tableGrob(cov_table_a, rows = NULL))

可以代替。


但也可以使用ggplot:

qplot(1:10, 1:10, geom = "blank") + 
  theme_void() +
  annotation_custom(
    grob = tableGrob(cov_table_a, rows = NULL), 
    xmin = -5, xmax = 10, ymin = 1, ymax = 10
  )

【讨论】:

  • 你是绝对正确的,一如既往,@Baptiste。我认为这只是一个最小的例子,实际的情节有更多的东西。
  • 我担心新的R画廊suggesting this strategy要画一张桌子
猜你喜欢
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多