Ggplot2 stat_binhex (): save bin radius when resizing graph

I would like to find a way to preserve the correct hexagons (all sides have equal length) when resizing hexbin graphs in ggplot2 without manually setting the bin width parameter.

To illustrate:

d <- ggplot(diamonds, aes(carat, price))+ stat_binhex(colour="white") try(ggsave(plot=d,filename=<some file>,height=6,width=8)) 

gives hexagons that at least look like regular eyes: ggplot2 stat_binhex plot1

and

 try(ggsave(plot=d,filename=<some other file>,height=6,width=12)) 

gives irregular hexagons: ggplot2 stat_binhex plot2

The documentation describes the bin width parameter (for example, binwidth = c(1, 1000) ), which determines the width of the cell. I need a function that, at a given chart size, returns the correct bin width settings to create the correct hexagons.

+4
source share
2 answers

Here you can dynamically adjust the width of the bin. I turned on the processing of the proportions of the portrait and the explicitly set axis boundaries.

 bins <- function(xMin,xMax,yMin,yMax,height,width,minBins) { if(width > height) { hbins = ((width/height)*minBins) vbins = minBins } else if (width < height) { vbins = ((height/width)*minBins) hbins = minBins } else { vbins = hbins = minBins } binwidths <- c(((xMax-xMin)/hbins),((yMax-yMin)/vbins)) return(binwidths) } 

For example, this code:

 h = 5 w = 5 yMin = min(diamonds$price) yMax = max(diamonds$price) xMin = min(diamonds$carat) xMax = max(diamonds$carat) minBins = 30 d <- ggplot(diamonds, aes(x = carat, y = price))+ stat_binhex(colour="white", binwidth = bins(xMin,xMax,yMin,yMax,h,w,minBins))+ ylim(yMin,yMax)+ xlim(xMin,xMax) try(ggsave(plot=d,filename=<some file>,height=h,width=w)) 

Productivity: graham jeffries - hexbin plot 1 And when we change the width:

 w = 8 d <- ggplot(diamonds, aes(x = carat, y = price))+ stat_binhex(colour="white", binwidth = bins(xMin,xMax,yMin,yMax,h,w,minBins))+ ylim(yMin,yMax)+ xlim(xMin,xMax) try(ggsave(plot=d,filename=<some file>,height=h,width=w)) 

graham jeffries - hexbin plot 2

Or change the height:

 h = 8 w = 5 d <- ggplot(diamonds, aes(x = carat, y = price))+ stat_binhex(colour="white", binwidth = bins(xMin,xMax,yMin,yMax,h,w,minBins))+ ylim(yMin,yMax)+ xlim(xMin,xMax) try(ggsave(plot=d,filename=<some file>,height=h,width=w)) 

graham jeffries - hexbin plot 3

We can also change the limits of x and y:

 h = 5 w = 5 xMin = -2 d <- ggplot(diamonds, aes(x = carat, y = price))+ stat_binhex(colour="white", binwidth = bins(xMin,xMax,yMin,yMax,h,w,minBins))+ ylim(yMin,yMax)+ xlim(xMin,xMax) try(ggsave(plot=d,filename=<some file>,height=h,width=w)) 

graham jeffries - hexbin plot 4

+4
source

Your choice is to set coord_fixed with the appropriate ratio so that the graph does not stretch to fit the size of the graphics device.

In this case, 5/17000 will be reasonable

 d <- ggplot(diamonds, aes(carat, price))+ stat_binhex(colour="white") + coord_fixed(ratio = 5/17000) 

Another option is to create bins and the ratio of the coordinate sizes, taking into account the aspect ratio of the device.

If the coordinate coefficient is not fixed (according to my first example), you cannot expect to stretch the same plot into a window that is 1.5 times wider without a stretched graph.

therefore, if you stretch the width 1.5 times, reduce the width of the bin at a size x 1.5 times

 d <- ggplot(diamonds, aes(carat, price))+ stat_binhex(colour="white",bin.widths = c((5/45),17000/30 )) 
+4
source

Source: https://habr.com/ru/post/1443139/


All Articles