2016-11-24 36 views
3

我想用ggplot繪製一張表,但似乎無法設法丟失row.names。刪除gridExtra中的rownames :: tableGrob

我曾嘗試:

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) 
+1

,請不要使用虛擬ggplots嵌入表的唯一目的! 'grid.draw(tableGrob())'是推薦的方法。 – baptiste

回答

7

注:使用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) 

enter image description here

+0

你一向是絕對正確的@Baptiste。我認爲這只是一個最小的例子,實際情節有更多的東西。 – Axeman

+1

我很擔心新的R gallery [建議使用此策略](http://www.r-graph-gallery.com/?s=tableGrob)繪製表格 – baptiste

+0

感謝編輯@baptiste。 – Axeman