r stat_contour incorrect fill with polygon

半城伤御伤魂 提交于 2019-11-30 22:26:51

This didn't fit in a comment, so posting as an answer:

stat_contour doesn't handle polygons that aren't closed very well. Additionally, there is a precision issue that crops up when setting the bins manually whereby the actual contour calculation can get freaked out (this happens when the contour bins are the same as plot data but aren't recognized as the same due to precision issues).

The first issue you can resolve by expanding your grid by 1 all around in every direction, and then setting every value in in the matrix that is lower than the lowest you care about to some arbitrarily low value. This will force the contour calculation to close all the polygons that would otherwise be open at the edges of the plot. You can then set the limits with coord_cartesian(xlim=c(...)) to have your axes flush with the graph.

The second issue I don't know of a good way to solve without modifying the ggplot code. You may not be affected by this issue.

@BrodieG Your answer is correct, but it's a bit difficult without some code.

Adding the following lines, with appropriate x,y values (these are a best guess), makes things clearer:

   xlim(-10, 60)+
   ylim(30, 120)+
   coord_cartesian(xlim=c(0, 50),ylim=c(40, 100))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!