In R, is there a way to constrain stat_density_2d to a polygon/shape in an underlying JPEG?

68 views Asked by At

In R, I am trying to run stat_density_2d on a dataset and include a jpeg (containing a polygon) underneath. However, I would like to constrain the analysis of stat_density_2d so that it fits completely within the polygon shown in the underlying jpeg. Right now, I have the results from stat_density_2d displaying outside of the polygon.

To help visualize, here is a picture of another similar example that I found online, however, my underlying image is not a map with associated coordinates. Example plot

I am not necessarily looking to just crop the analysis to the polygon shape. I would like to constrain the density plot analysis to the polygon's extent.

Any help would be appreciated, even general steps for how to move forward.

See explanation above.

0

There are 0 answers