減少在x
尺寸binwidth這裏的動態調整binwidth該溶液中。我已經包括處理縱向縱橫比和明確說明的軸限制。
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)
}
例如下面的代碼:
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))
產量: 當我們改變寬度:
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))
或改變高度:
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))
我們也可以改變x和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))
+1在一個一般化的功能很好地加以包裝。 – mnel