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,


enter image description here

  • 3
    $\begingroup$ Would you please set a seed so your code is actually reproducible? Also, please label your axes and title your plot. $\endgroup$ – Waldir Leoncio Aug 6 '16 at 8:49
  • $\begingroup$ @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. $\endgroup$ – Lin Ma Aug 7 '16 at 0:07
  • 1
    $\begingroup$ 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. $\endgroup$ – Hugh Aug 7 '16 at 1:16
  • $\begingroup$ Thanks @Hugh, vote up. What do you mean darker colored region? Do you mean the blue region I am using red circle to highlight? :) $\endgroup$ – Lin Ma Aug 7 '16 at 4:08
  • 1
    $\begingroup$ @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. $\endgroup$ – 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))

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

Contur Plot of this example

  • $\begingroup$ 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. :) $\endgroup$ – Lin Ma Aug 8 '16 at 19:43
  • 1
    $\begingroup$ @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]. $\endgroup$ – Qaswed Aug 12 '16 at 8:05
  • $\begingroup$ Nice answer Qaswed, mark your reply as answer. $\endgroup$ – Lin Ma Aug 14 '16 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.