R 禁止显示“;不适用;在桌子上
我有一个表,我想使用tableGrob与ggplot2绘图一起绘图。出于输出目的,我想禁止打印NA 例如:R 禁止显示“;不适用;在桌子上,r,ggplot2,R,Ggplot2,我有一个表,我想使用tableGrob与ggplot2绘图一起绘图。出于输出目的,我想禁止打印NA 例如: 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)
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")
library(RGraphics)#支持CRAN上的“R图形”一书
图书馆(gridExtra)
tab正在打印的数据表存储在元素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
正如将NA
替换为“
会将列转换为字符和松散格式一样,首先,数据帧列应转换为字符,然后将NA
值替换,并将数据结构转换回数据帧(以消除引号)
g1$dun幸运的是,tableGrob的内置格式依赖于表中包含的数据类型。因此,将NA值设置为“”会将整个列强制转换为字符串类型。这导致像3.0这样的东西被格式化为3,这不是我想要的。此外,如果我有较大的值并用逗号打印出来(例如3456789),这些值也会遭受类似的命运。你说得对。这很难看,但它完成了任务。谢谢
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")