stat_density_2d doesnt generate contours that match raw data

218 Views Asked by At

I have a plot showing the points in my data (image 1) geom_point() with stat_density_2d overlayed and a contour plot produced using stat_density_2d (image 2) stat_density_2d on its own, same dataset

The contours clearly don't represent the raw data very well. I have used the same code to generate other contour plots that fit the data perfectly (image 3) different dataset, identical code, points are well represented by contours

The code I am using is:

SolidReg<-ggplot(RhyShp[,c(13,15)], aes(x=Solidity, y=Reg) ) + 
    stat_density_2d(aes(fill = ..level..), geom = "polygon") +
    labs(x = "Solidity", y = "Regularity") +
    theme_classic()

RhyShp is the dataframe from my file 5_102_Rhy.csv used to generate images 1 and 2.

Does anyone know why the contour plot doesn't reflect the dataset? I am not sure why the code would work for one csv but not another....

thanks!

1

There are 1 best solutions below

0
On

Turns out this was an issue with the data containing multiples of identical values which skewed the density without being discernible in the geom_point() plot. Once these duplicates were removed the density plot reflected the true density of the data.