I'm having a very strange problem with the persp()
function inside R. I'm using the split.screen()
function to arrange three plots. I've produced a MWE below:
f <- function(x,y) {
return(x*y)
}
u <- seq(0,5, by = 0.1)
v <- seq(0,5, by = 0.1)
z <- outer(u, v, f)
persp(u,v,z, ticktype="detailed", col = rgb(0.2,0.6,1))
pdf("~/Desktop/test.pdf", width = 10, height = 10) # adjust path as necessary
split.screen( figs = c( 2, 1 ) )
split.screen( figs = c( 1, 2 ) )
screen(2)
persp(u,v,z, ticktype="detailed", col = rgb(0.2,0.6,1))
screen(3, new = FALSE)
persp(u,v,z, ticktype="detailed", col = rgb(0.2,0.6,1))
screen(4, new = FALSE)
persp(u,v,z, ticktype="detailed", col = rgb(0.2,0.6,1))
close.screen(all = TRUE)
dev.off()
Looking at the resulting PDF, the surfaces look quite dark, especially in the left side. When I print the file they look even darker (too dark).
I've also attached a screenshot of the plot zoom of one of the 3 plots in RStudio. Looks much better. I'd like all three plots to look like the plot zoom in RStudio, even when printed.
Does anyone know how to do that?
Cheers!
You're seeing the effect of the gridlines being closer together in the smaller versions of the plot. If you shrink the graph created with RStudio's Zoom down to the same size as the other graphs, it looks just as dark as the ones created with the pdf device. The problem is that the gridlines appear too close together due to foreshortening at low viewing angles (for example, the back corner of your plots). You can see this if you zoom in on the plot:
Here are some options to improve the output:
Reduce line width to get finer gridlines. This will increase the relative area of the blue squares, making the whole surface appear brighter, and will particularly reduce the darkening in the far corner due to "bunching up" of the grid lines as a result of the shallow viewing angle.
persp(u,v,z, ticktype="detailed", col = rgb(0.2,0.6,1), lwd=0.5)
I found that the pdf
device didn't seem to act on the lwd
argument, but the cairo_pdf
device does (although the gridlines still come out thicker than they appear in the RStudio graphics window). lwd=0.5
worked well for viewing in the RStudio graphics window (and also for output to a png file). For cairo_pdf output, lwd=0.1
resulted in thinner lines than the default linewidth, but not as thin as I would have liked. However, when I set the linewidth lower than 0.07, no plot appeared at all. Here's the same plot as above, but with linewidth set to 0.1 and using RStudio's PDF output:
Reduce number of gridlines by using a larger value of by
in your seq
functions.
Use a brighter color for the perspective surface.
Use a lighter color for the gridlines (e.g., set border="gray40"
or even border="white"
in the persp
function).
For example, here's the result of the following call to persp
. The background color is a little brighter and the gridlines are thinner and are white instead of black:
png("~/persp.png", 500,500)
persp(u,v,z, ticktype="detailed", col=hcl(240,100,80), border="white", lwd=0.5)
dev.off()