0

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!

Foxxy
  • 3
  • 3
  • It looks like there just aren't any points in the center. Or are you looking to fill in that area with red? – shadowtalker Sep 27 '14 at 13:14
  • Yes I would like to fill the area with the appropriate density color which would be red in this case. I mean, it should be doing so automatically but for some reason the highest density area is left out. And yes there are many points there, I checked without the other colors and in Excel. – Foxxy Sep 27 '14 at 15:21
  • 1
    Welcome to SO. I know you're worried about your thesis, but you need to take a deep breath and properly look at your code. Right now it looks like you are just typing anything to see what works without thinking about it. Start by reading the example on the `?densCols` help page. And to make it easier for us to help you, please provide some data for us to be able to reproduce the problem. – Richie Cotton Sep 27 '14 at 19:11

1 Answers1

0

Two solutions here. In both cases, if you want to display the points (rather than the more standard density contours; see ggplot2 example), then it is important that you deal with overplotting by making your points transparent.

#Data import and cleaning
d <- read.table(
  "http://pastebin.com/raw.php?i=DcFdZ5Rm", 
  header = TRUE, 
  stringsAsFactors = FALSE
)
d[] <- lapply(d, as.numeric)

# Draw the plot using ggplot.  It's nicer. 
# Here's a scatterplot with density contours.
library(ggplot2)
(p_contours <- ggplot(d, aes(Ti, Zr)) +
  geom_point(alpha = 0.1) +
  geom_density2d()
)

# Here's a heatmap. Explore different binwidth values.
(p_heatmap <- ggplot(d, aes(Ti, Zr)) +
  geom_bin2d(binwidth = c(0.001, 0.001))
)

#If you insist on using base graphics, try something like
library(scales)
col <- densCols(d$Ti, d$Zr, colramp=colorRampPalette(c("grey67", "blue")))    
col <- alpha(col, 0.1) # Make points transparent
plot(d$Ti, d$Zr, col = col, pch = 16)    

why is the dense part of the plot white?

Because you chose a black and white colour ramp, with white representing the highest density region. Because you only specified colours for the first 32 elements in density, so some points have a missing colour value.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • I'm sorry for coming off so panicked but this has been a major problem for me. I'm forced to use a program that I don't know anything about so I turned here for help. I tried to read every help section there is on kernel functions but I can't understand everything so I patched together a code that I found on the internet. Is it possible to upload a txt file here with some data? There are too many points and I can't really copy them into the comments or the original post. I appreciate for your time and all the help I can get! – Foxxy Sep 27 '14 at 23:54
  • Provide a subset of your real data (the `dput` function will let you provide it in a form for reuse here), or create some synthetic data that reproduces the problem (with `rnorm` perhaps). More help here: http://stackoverflow.com/q/5963269/134830 – Richie Cotton Sep 28 '14 at 08:30
  • I have a large dataset which is the whole point of this plot so I took a different approach. There is an updated section at the bottom of the original post above where I included a command which can be used to import my data directly into R. Hope this helps! – Foxxy Sep 28 '14 at 10:50
  • The 'ggplot' is nice but it's not drawing the density. I would like to have a color gradient from blue to red with each color representing a different density like the one I already have, but I need to remove the white empty space and fill it with red like it should be. If I change the 'colourRampPalette', nothing happens. – Foxxy Sep 29 '14 at 12:09
  • The white space exists because you only specified colours for the first 32 elements in density. Look at the `col` column in your data frame - it contains many missing values. And this code is getting tricky because you are trying to do something non-standard, that doesn't make much sense. If you want to show densities, then add contours to your scatterplot (as I demonstrated) or draw a heatmap. These are standard, good, solutions, so I think that it is a bad idea to reinvent the wheel. – Richie Cotton Sep 30 '14 at 05:25
  • Ok thanks, I think I understand. Any tips on how to create an easy and good heat map? – Foxxy Sep 30 '14 at 09:27
  • If my answer was useful, you can upvote it by clicking the up arrow next to the score, and mark it as the accepted answer by clicking the tick. – Richie Cotton Oct 02 '14 at 12:29
  • Great, thank you! I can't upvote it because my reputation is too low but I marked the tick :) – Foxxy Oct 03 '14 at 15:18