I'm trying to construct a density heatmap in R, using ggplot2
and stat_density2d
. While it does give me a density plot over 2 axes, it produces strange triangular spaces next to the expected heatmap.
I'm following this example, hence the following code produces the heatmap itself (without scatter):
dfFilter <- data.frame(matrix(runif(2000, 0.0, 1.0),nrow=1000))
# HEATMAP
ggplot(dfFilter,aes(x= X1,y= X2))+
stat_density2d(aes(alpha=..level..), geom="polygon")
My result looks quite as expected, but has some unexpected traingles. It looks like R is connecting dots, but suddenly jumps to the other side of the plot to continue.
Anyone who knows what the reason could be, and how to solve it? Thanks a lot!