Reputation: 4389
I have a table that I'd like to plot alongside a ggplot2 plot using a tableGrob. For output purposes, I would like to suppress NA from being printed.
Example:
library(RGraphics) # support of the "R graphics" book, on CRAN
library(gridExtra)
tab <- head(iris)
tab[1,2] <- NA # set a couple values to NA for example purposes
g1 <- tableGrob(tab)
#"Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
g2 <- qplot(Sepal.Length, Petal.Length, data=iris, colour=Species)
grid.arrange(g1, g2, ncol=1, main="The iris data")
Upvotes: 2
Views: 1299
Reputation: 98579
Data table you are plotting is stored in element g1$d
.
g1$d
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 NA 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
As just replacing NA
with ""
will convert column to character and loose formatting, first, data frame columns should be converted to character, then NA
value replaced and data structure converted back to data frame (to get rid of quotes).
g1$d<-apply(g1$d,2,as.character)
g1$d[is.na(g1$d)]<-""
g1$d<-as.data.frame(g1$d)
g1$d
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
grid.arrange(g1, g2, ncol=1, main="The iris data")
Upvotes: 2