4

ggplot2およびを使用knitrして、右側の凡例を含む散布図を公開しています。凡例は縦横比に含まれているため、デフォルトのテーマに示されているように、プロットの「直角」が崩れます。凡例テキストが「a」と「b」より少し長くなると、グラフは「正方形」ではなく「長い長方形」になります。

グラフを「二乗」したままにしたいので、ggplot2グラフの縦横比から凡例を除外したいと思います。次の.Rprofile情報を使用して、ggplot2テキストを大きくし、軸タイトルの周囲にスペースを多くして、低色のグラフを強制的に生成します。

theme_set(theme_bw(16))
theme_update(
  axis.title.y = element_text(angle = 90, vjust = -.25),
  axis.title.x = element_text(vjust = -1),
  plot.margin = unit(c(1,1,1,1), "cm")
)

縦横比から凡例を除外するためにここに追加できるものはありますか? と の操作は、coord_equalとのオプションとcoord_fixed同様に、これまでのところ失敗しています。ご協力いただきありがとうございます!fig.widthfig.height

編集:実際の例を削除し、完全なサンプルコードで以下の質問に回答しました(回答の承認が遅れて申し訳ありません)。

4

1 に答える 1

5

設定coord_fixed()はトリックを行う必要があります:

library(ggplot2)
library(gridExtra)  ## for grid.arrange()

## Create some data with one longer label
cars <- transform(mtcars, 
           cyl = factor(cyl, labels=c("4","6","8 is big")))


## Compute ratio needed to make the figure square
## (For a taller narrow plot, multiply ratio by number > 1; 
##  for a squatter plot, multiply it by number in the interval (0,1))    
ratio <- with(cars, diff(range(mpg))/diff(range(wt)))

## Make plots with and without a legend
a <- ggplot(cars, aes(mpg, wt)) + 
     geom_point() + coord_fixed(ratio=ratio)

b <- ggplot(cars, aes(mpg, wt, color=cyl)) + 
     geom_point() + coord_fixed(ratio=ratio)

## Plot them to confirm that coord_fixed does fix the aspect ratio
grid.arrange(a,b, ncol=2)

ここに画像の説明を入力してください

于 2013-01-31T17:47:43.407 に答える