Reputation: 13
I am trying to visualize some matrices using levelplot() function in the lattice package. I need the matrices presented in a grid (6 plots in the first row, 6 plots in the 2nd row, 4 plots in the 3rd row). And I need to save them as a png. I wrote a code to do that, and everything looks great in the R Markdown chunk output. But when I try knitting or saving the output as .png, the output looks messy (plots look too small, distances between them too big). Can anybody give me a tip how to remedy that?
My code is:
library(lattice)
library(viridis)
m1 <- rbind(c(0,3,4.5,4),c(3,0,3.5,4.8),c(4.5,3.5,0,5.2),c(4,4.8,5.2,0))
m2 <- rbind(c(0,2,2.5,3),c(2,0,3.2,3),c(2.5,3.2,0,3.2),c(3,3,3.2,0))
m3 <- rbind(c(0,4,5.5,5),c(4,0,4.5,5.8),c(5.5,4.5,0,5.2),c(5,5.8,5.2,0))
m4 <- rbind(c(0,2.5,4.5,4),c(2.5,0,4,4.8),c(4.5,4,0,5.2),c(4,4.8,5.2,0))
m5 <- rbind(c(0,3,5,6),c(3,0,3.5,5.2),c(5,3.5,0,5.2),c(6,5.2,5.2,0))
m6 <- rbind(c(0,7,5.5,6),c(7,0,6.5,4.9),c(5.5,6.5,0,5.2),c(6,4.9,5.2,0))
m7 <- rbind(c(0,1,2,2),c(1,0,1,2),c(2,1,0,5),c(2,2,5,0))
m8 <- rbind(c(0,3,5.8,4),c(3,0,4.2,5.8),c(5.8,4.2,0,4.1),c(4,5.8,4.1,0))
m9 <- rbind(c(0,0,0,1),c(0,0,1,1),c(0,1,0,0),c(1,1,0,0))
m10 <- rbind(c(0,2.3,4,4),c(2.3,0,3.5,3),c(4,3.5,0,2),c(4,3,2,0))
m11 <- rbind(c(0,1.1,1.2,1.1),c(1.1,0,1.5,1.3),c(1.2,1.5,0,0.9),c(1.1,1.3,0.9,0))
m12 <- rbind(c(0,5,6,7),c(5,0,6.7,4.2),c(6,6.7,0,5.2),c(7,4.2,5.2,0))
m13 <- rbind(c(0,4.6,4.5,7),c(4.6,0,5.9,2.5),c(4.5,5.9,0,5.2),c(7,2.5,5.2,0))
m14 <- rbind(c(0,7.9,6.2,7.9),c(7.9,0,7,6.7),c(6.2,7,0,5.2),c(7.9,6.7,5.2,0))
m15 <- rbind(c(0,2.3,2,3.7),c(2.3,0,3,1.9),c(2,3,0,3),c(3.7,1.9,3,0))
m16 <- rbind(c(0,0.7,2,2.1),c(0.7,0,0.8,1.6),c(2,0.8,0,2),c(2.1,1.6,2,0))
col_l <- colorRampPalette(c(viridis(10)))
color_steps <- seq(0,8, by=0.01)
#trellis.device('png', file = 'toy_matrices.png')
plot.new()
par(mfrow=c(3,6), cex=2, oma=c(0,0,3,0), mar = c(2, 2, 2, 2))
print(levelplot(m1, at=color_steps,col.regions=col_l, xlab="mat1", ylab=NULL, colorkey=TRUE), split=c(1, 1, 6, 3), newpage=FALSE)
print(levelplot(m2, at=color_steps,col.regions=col_l, xlab="mat2", ylab=NULL, colorkey=TRUE), split=c(2, 1, 6, 3), newpage=FALSE)
print(levelplot(m3, at=color_steps,col.regions=col_l, xlab="mat3", ylab=NULL, colorkey=TRUE), split=c(3, 1, 6, 3), newpage=FALSE)
print(levelplot(m4, at=color_steps,col.regions=col_l, xlab="mat4", ylab=NULL, colorkey=TRUE), split=c(4, 1, 6, 3), newpage=FALSE)
print(levelplot(m5, at=color_steps,col.regions=col_l, xlab="mat5", ylab=NULL, colorkey=TRUE), split=c(5, 1, 6, 3), newpage=FALSE)
print(levelplot(m6, at=color_steps,col.regions=col_l, xlab="mat6", ylab=NULL, colorkey=TRUE), split=c(6, 1, 6, 3), newpage=FALSE)
print(levelplot(m7, at=color_steps,col.regions=col_l, xlab="mat7", ylab=NULL, colorkey=TRUE), split=c(1, 2, 6, 3), newpage=FALSE)
print(levelplot(m8, at=color_steps,col.regions=col_l, xlab="mat8", ylab=NULL, colorkey=TRUE), split=c(2, 2, 6, 3), newpage=FALSE)
print(levelplot(m9, at=color_steps,col.regions=col_l, xlab="mat9", ylab=NULL, colorkey=TRUE), split=c(3, 2, 6, 3), newpage=FALSE)
print(levelplot(m10, at=color_steps,col.regions=col_l, xlab="mat10", ylab=NULL, colorkey=TRUE), split=c(4, 2, 6, 3), newpage=FALSE)
print(levelplot(m11, at=color_steps,col.regions=col_l, xlab="mat11", ylab=NULL, colorkey=TRUE), split=c(5, 2, 6, 3), newpage=FALSE)
print(levelplot(m12, at=color_steps,col.regions=col_l, xlab="mat12", ylab=NULL, colorkey=TRUE), split=c(6, 2, 6, 3), newpage=FALSE)
print(levelplot(m13, at=color_steps,col.regions=col_l, xlab="mat13", ylab=NULL, colorkey=TRUE), split=c(1, 3, 6, 3), newpage=FALSE)
print(levelplot(m14, at=color_steps,col.regions=col_l, xlab="mat14", ylab=NULL, colorkey=TRUE), split=c(2, 3, 6, 3), newpage=FALSE)
print(levelplot(m15, at=color_steps,col.regions=col_l, xlab="mat15", ylab=NULL, colorkey=TRUE), split=c(3, 3, 6, 3), newpage=FALSE)
print(levelplot(m16, at=color_steps,col.regions=col_l, xlab="mat16", ylab=NULL, colorkey=TRUE), split=c(4, 3, 6, 3), newpage=FALSE)
title("Matrices", outer=T)
#dev.off()
In R Markdown window the chunk output looks like this:
However, the saved .png file looks like this:
I would appreciate any advice.
Upvotes: 0
Views: 62