gpt4 book ai didi

r stat_contour 多边形填充不正确

转载 作者:行者123 更新时间:2023-12-02 01:51:19 26 4
gpt4 key购买 nike

当我将stat_contour与多边形一起使用时,某些区域不应该被填充,因为那里没有数据,我在图中标记了它们。有谁知道如何避免这种情况?另外,坐标轴和绘图区域之间有空格,如何去掉?!

这是绘图代码:

plot_contour <- function (da, native ) {
h2d<-hist2d(da$germ_div,da[[native]],nbins=40,show=F)
h2d$counts<-h2d$counts+1
counts<-log(h2d$counts, base=10)
rownames(counts)<-h2d$x
colnames(counts)<-h2d$y
counts<-melt(counts)
names(counts)<-c('x','y','z')
ggplot(counts,aes(x,y))+

stat_contour(expand=c(0,0),aes(z=z,fill=..level..),geom='polygon')+
stat_contour( data=counts[counts$x<=75,],aes(z=z,fill=..level..),bins=50,geom='polygon')+
scale_fill_gradientn(expand=c(0,0),colours=rainbow(1000),
limits=c(log(2,base=10),4),na.value='white',guide=F)+
geom_contour(aes(z=z,colour=..level..),size=1.5)+
scale_color_gradientn(colours=rainbow(30),limits=c(log(2,base=10),4),na.value='white',
guide=F) + theme_bw()+
scale_x_continuous(expand=c(0,0),limits=c(0,50))+
scale_y_continuous(expand=c(0,0),limits=c(40,100))+
labs(x=NULL, y=NULL, title=NULL)+
theme(axis.text.x = element_text(family='Times', colour="black", size=20, angle=NULL,
hjust=NULL,vjust=NULL,face="plain"),
axis.text.y = element_text( family='Times', colour="black", size=20,angle=NULL,
hjust=NULL,vjust=NULL,face="plain")
)

}

da<-read.table('test.txt',header=T)
i<-'test'

plot_contour(da,i)

最佳答案

这不适合评论,因此作为答案发布:

stat_contour 不能处理未很好闭合的多边形。此外,手动设置箱时会出现精度问题,从而实际的轮廓计算可能会被吓坏(当轮廓箱与绘图数据相同但由于精度问题而未被识别为相同时,就会发生这种情况) .

您可以通过在每个方向上将网格扩展 1 来解决第一个问题,然后将矩阵中低于您关心的最低值的每个值设置为任意低值。这将强制轮廓计算关闭所有在绘图边缘处打开的多边形。然后,您可以使用 coord_cartesian(xlim=c(...)) 设置限制,以使轴与图形齐平。

第二个问题我不知道有什么好的方法可以在不修改ggplot代码的情况下解决。您可能不会受到此问题的影响。

关于r stat_contour 多边形填充不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com