4

Binwidth パラメータを手動で調整せずに ggplot2 で hexbin プロットのサイズを変更するときに、正六角形 (すべての辺の長さが等しい) を維持する方法を見つけたいと思います。

説明する:

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

少なくとも目には規則的に見える六角形が得られます。ggplot2 stat_binhex プロット 1

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

不規則な六角形を生成します。ggplot2 stat_binhex プロット 2

ドキュメントでは、binwidth = c(1, 1000)ビンの幅を指定する binwidth パラメータ (例: ) について説明しています。任意のプロット サイズを指定すると、正六角形を作成するための適切なビン幅設定を返す関数が必要です。

4

2 に答える 2

4

coord_fixedプロットがグラフィックス デバイスのサイズを超えないように、適切な比率で設定することを選択します。

この場合5/17000、合理的に見えるでしょう

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

もう 1 つのオプションは、デバイスの次元の比率を考慮して、ビンの幅と座標次元の比率を作成することです。

座標比が固定されていない限り (最初の例のように)、同じプロットを 1.5 倍の幅のウィンドウに引き伸ばすことは期待できません。プロットが引き伸ばされているようには見えません。

xしたがって、幅を 1.5 倍に伸ばす場合は、次元 のビン幅を 1.5 倍に減らします。

d <- ggplot(diamonds, aes(carat, price))+ 
   stat_binhex(colour="white",bin.widths = c((5/45),17000/30 ))
于 2012-10-31T03:53:45.820 に答える
4

ビン幅を動的に調整するソリューションは次のとおりです。ポートレートの縦横比と明示的に指定された軸の制限の処理を含めました。

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

収量: グラハム・ジェフリーズ - hexbin プロット 1 幅を変更すると:

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

グラハム・ジェフリーズ - hexbin プロット 2

または高さを変更します。

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

グラハム・ジェフリーズ - hexbin プロット 3

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

グラハム・ジェフリーズ - hexbin プロット 4

于 2012-10-31T15:02:20.933 に答える