I've searched around for a while but I still can't solve this minor issue in which I would like to change the decimal place of numbers in the table created by tablegrob()
. Doing so would make the entire view look better, as seen below:
This is the code:
a<-as.vector(matrixA) #matrices A-D contain randomly generated NA and
b<-as.vector(matrixB) #numerical values
c<-as.vector(matrixC)
d<-as.vector(matrixD)
data <- cbind(a, b, c, d)
plot <- melt(data)
colnames(plot)[2]<-"variable"
plot<-plot[,-1]
levels(df$variable) <- c("A", "B", "C",
"D")
xlabs <- paste(levels(plot$variable),"\n(N=",
colSums(!is.na(data)),")",sep="")
boxplot1<-ggplot(na.omit(plot),aes(x=variable,y=value,color=variable))+
geom_boxplot()+scale_x_discrete(labels=xlabs)+
xlab("X") + ylab("Y")+
ggtitle("Title")
#create inset table
multi.fun <- function(x) {
c(Mean = mean(x,na.rm=TRUE), Median = median(x,na.rm=TRUE),
IQR = IQR(x,na.rm=TRUE))}
sum.table<-data.frame(lapply(data.frame(a,b,c,d), multi.fun))
colnames(sum.table)<- c("A", "B", "C",
"D")
my_table <- tableGrob(sum.table, **digits=2**, gpar.coretext = gpar(fontsize=8),
gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8))
#Create the viewports, push them, draw and go up
grid.newpage()
vp1 <- viewport(width = 0.75, height = 1, x = 0.375, y = .5)
subvp <- viewport(width = 0.3, height = 0.3, x = 0.75, y = 0.20)
print(boxplot1, vp = vp1)
upViewport(0)
pushViewport(subvp)
grid.draw(my_table)
I tried adjusting the values using digits
in tablegrob
, which didn't work. I also tried options(digits=2) , which also didn't work. Any thoughts or ideas?
colnames(sum.table)<- c("A", "B", "C","D")
***sum.table<-format(sum.table[1:3,1:4], digits=2)***
my_table <- tableGrob(sum.table,
gpar.coretext = gpar(fontsize=8), gpar.coltext=gpar(fontsize=8),
gpar.rowtext=gpar(fontsize=8))
Formatting the data.frame before using tableGrob using sum.table<-format(sum.table[1:3,1:4], digits=2)
, I obtain the desired outcome:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.