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

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

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