I'm a non-programmer who badly needs to plot a multivariate kernel density function with two variables against each other to better understand my data. After searching the internet I found a code which I used to insert my data and create the desired function.
This is my current code:
mydata = read.csv("J:/LKAB Thesis/Plots/Kernel/81241.txt", header = TRUE, sep = "\t")
x1 <- mydata[,11]
x2 <- mydata[,23]
df <- data.frame(x1,x2)
x <- densCols(x1,x2, colramp=colorRampPalette(c("black", "white")))
df$dens <- col2rgb(x)[1,] + 1L
cols <- colorRampPalette(c("#000099", "#00FEFF", "#45FE4F",
"#FCFF00", "#FF9400", "#FF3100"))(32)
df$col <- cols[df$dens]
plot(x2~x1, data=df[order(df$dens),], pch=20, col=col, cex=1)
And this is the resulting plot:
https://i.stack.imgur.com/KToCw.jpg
This is exactly what I need, but why is the dense part of the plot white? It should be filled with a nice smooth red color to indicate the high density. How do I fix this?? If I change the colorRampPalette
to a higher value like 256, the hole is filled but the rest of the plot is much uglier and less detailed which I don't want.
Please help, I need this to finish my thesis but bear in mind that I'm a beginner and don't really know anything about R. Unfortunately I need to use it for this plot!
Here is some sample data which can be used for this example. These are measured concentrations of two elements, Ti and Zr which I want to plot against each other and see the densities of the points.
d <- read.table("http://pastebin.com/raw.php?i=DcFdZ5Rm")
Thanks!