'Using transparency settings on a kernel plot, R
Hi I am trying to plot and overlay multiple kernel density estimates into a single plot (using KS library). Since I want to overlay multiple kernels I am "playing" with the transparency color settings so that the final plot is easier to understand. However, for some reason when I plot it, it shows a grid pattern with the color color/transparency filling which I don't know how to remove so that the color is all uniform...
These are the options that I use to export my plot as a PDF,
pdf(paste(database$species[1],".pdf",sep=""),width=11,height=8,paper="a4r",pointsize=15)
par(mfrow=c(1,1))
par(mar=c(5,4,4,3))
This is the code that I use to make the plot and overlay it with others,
# plot of KUD-Depth utilization
plot(ddhat.day,cont=c(95),lwd=1,add=F,display="filled.contour2",col=c(NA,"#8B000040"),xaxs="i",yaxs="i",
plot(ddhat.night,cont=c(95),lwd=1.5,add=TRUE,display="filled.contour2",col=c(NA,"#00008020"),
xlab="",ylab="",las=1,ann=F,bty="l",cex=0.6,yaxs="i",xaxs="i")
xlab="",ylab="",las=1,ann=F,bty="l",cex=0.6,xlim=c(0,max(dd[,1]+dd[,1]*0.6)),ylim=c(50,-10))
plot(ddhat.day,cont=c(50),add=T,display="filled.contour2",col=c(NA,"#FF000040"),lty=2,lwd=1,
xlab="",ylab="",cex=0.6)
plot(ddhat.night,cont=c(50),add=TRUE,display="filled.contour2",col=c(NA,"#00BFFF40"),lwd=1.5,
xlab="",ylab="",cex=0.6)
plot(ddhat.day,cont=c(0,0),drawpoints=TRUE,col="black",ptcol="grey15",cex=0.45,add=TRUE)
plot(ddhat.night,cont=c(0,0),drawpoints=TRUE,col="black",ptcol="grey15",cex=0.45,add=TRUE,pch=4)
dev.off()
If anybody has an idea why when I use this transparency option for color it shows a grid too and ho to remove it so it shows a smooth surface that would be fantastic!
Solution 1:[1]
You have a really weird way of specifying col. Instead of those NAs which I'm pretty sure aren't supposed to be there, try using the rgb function. It takes (at least) 4 arguments, which are the red, green, blue and alpha (transparency) channels, expressed as fractions.
col=rgb(0, 0, 0, 1) # black
col=rgb(0, 1, 1, .5) # green + blue = cyan, 50% opaque
col=rgb(1, 0, 0, .2) # red, 20% opaque = 80% transparent
Solution 2:[2]
This is a pretty old question but I thought I'd throw my response out there for future reference. The grid that you are seeing (if it's the grid I think you are referring to) is the result of creating a raster graphic in a pdf file. The pdf does a poor job of rendering the raster. If you want to make the grid (really thin white lines, right?) go away, try saving the file as a native raster file type (eg., jpeg(),tiff).
In most cases, outputting an R graphic as a pdf() works great because then you have a nice, scalable vector graphic for which you don't have to worry about resolution. However, when you create figures like an image plot sometimes it gets wonky (this only happens on my Mac though and not on my PC).
Some code below may illustrate. If you run the pdf version (on a mac) you will get the figure below. If you run the jpg version it will go away.
library(MASS)
library(ks)
data(iris)
fhat <- kde(x=iris[,1:2])
jpeg("test.jpg")
plot(fhat, display="filled.contour2", cont=seq(10,90,by=10))
dev.off()
pdf("test.pdf")
plot(fhat, display="filled.contour2", cont=seq(10,90,by=10))
dev.off()

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hong Ooi |
| Solution 2 | Jordan |
