user3566784
user3566784

Reputation: 41

r stat_contour incorrect fill with polygon

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)

Upvotes: 4

Views: 1211

Answers (2)

Grubbmeister
Grubbmeister

Reputation: 877

@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))

Upvotes: 0

BrodieG
BrodieG

Reputation: 52647

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.

Upvotes: 4

Related Questions