r stat_contour用多边形填充不正确 [英] r stat_contour incorrect fill with polygon

查看:134
本文介绍了r stat_contour用多边形填充不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是绘图代码:

  plot_contour<  -  function(da,native){
h2d< -hist2d(da $ germ_div,da [[native]],nbins = 40,show = F)
h2d $计数< -h2d $计数+ 1
计数< -log(h2d $计数,基数= 10)
rownames(计数)< -h2d $ x $ b $ (计数)< -h2d $ y
计数< -melt(计数)
名称(计数)< -c('x','y','z')
ggplot(counts,aes(x,y))+

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







$ b $ j $ =

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

plot_contour(da,i)


解决方案

这不适合发表评论,答案:

stat_contour不处理未关闭的多边形。此外,当手动设置箱子时会出现精度问题,从而导致实际的轮廓计算出现误差(这发生在轮廓箱与图数据相同但由于精度问题未被识别为相同时发生) 。

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

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


When I use stat_contour with polygon, some regions should not be filled because there is no data there, i marked them in the figure. Does anyone know how to avoid that? In addition, there is space between axis and plot region, how to remove it?!

Here is the plotting code:

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)

解决方案

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.

这篇关于r stat_contour用多边形填充不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆