# read density diagram generated in R

Not sure if I read the density diagram correctly from R, I think it means overall, most a and b happens in the dark red area, and for specific a values, for example, if a is 0.1, most b values are in the small red circle area I drawn, and when a value is 0.2, most b values are in the bigger red ellipse area I drawn?

Post the density diagram and sample code,

b<-log10(rgamma(1000,6,3))
a<-log10((rweibull(1000,8,2)))
density<-kde2d(a,b,n=100)
filled.contour(density,color.palette=colorRampPalette(c('white','blue','yellow','red','darkred')))


• Would you please set a seed so your code is actually reproducible? Also, please label your axes and title your plot. – Waldir Leoncio Aug 6 '16 at 8:49
• @WaldirLeoncio, thanks for the comments and vote up. I updated my post to change x y to a b so that my statement aligns with code. What do you mean set a seed? Actually it does not matter which specific data set, my question is about in general how to read density diagram correctly. Given the density diagram I posted, wondering if it is correct for my description in above post? Thanks. – Lin Ma Aug 7 '16 at 0:07
• You are reading the diagram correctly. If 'a' is on the x axis then when a=0.1 most b values are in the darker colored region vertically above a=0.1. – Hugh Aug 7 '16 at 1:16
• Thanks @Hugh, vote up. What do you mean darker colored region? Do you mean the blue region I am using red circle to highlight? :) – Lin Ma Aug 7 '16 at 4:08
• @LinMa that was a bad choice of words by me. In the two red circles you have white-dark blue colours and the b values in the most darkest shade of blue are most common. Of course if you look at a=0.35 then there are yellow and blue colours so the b values in the yellow region are most common. – Hugh Aug 7 '16 at 12:00

Yes, you are reading the diagram correctly. Regarding your question in the comments, "if darker color area (dark-red in my example) means a and b has higher correlation, or not?": Darker color area does not means a higher correlation. It just means that more data are observed in this area.

To make this last point more clear, I generate two independent and uncorrelated variables x and y. As the are uncorrelated, the dark area cannot show correlation. (PS: I think axis labels are a must have for statistical graphs, so a added them (and a title))

library(MASS)
set.seed(123)
x = rnorm(1000, mean = 10)
y = rnorm(1000, mean = 0)
density <- kde2d(x, y, n = 100)
filled.contour(density,
color.palette = colorRampPalette(c('white', 'blue', 'yellow', 'red', 'darkred')),
xlab = "x", ylab = "y", main = "contour plot of x and y")


• Thanks Qaswed, vote up. To read the density diagram you plot above, my reading is, when x enters into region (roughly) [9.5, 10.5], y enters into region dark-red [-1, 1]. There are correlation there. It seems. If I am wrong for any descriptions above, please feel free to correct me. :) – Lin Ma Aug 8 '16 at 19:43
• @LinMa, The dark red area just says that there are many observations with a value of x in [9.5, 10.5] and y in [-1, 1]. Its sound a bit odd to me to say that x or y "enters into a region". How I would read it, would be: if x element [9.5, 10.5], the values of y are likely to be in [-1, 1]. The crucial point is, that the probability of y to be in [-1, 1] is independent of the value/range of x (as they are independent). For x = 8 you have the exact same probability of y to be in [-1, 1]. – Qaswed Aug 12 '16 at 8:05