71

ここでの問題は少し明白だと思います。凡例を「プロット領域」の左上隅に配置 (ロック) したいと思います。c(0.1,0.13) などを使用することは、いくつかの理由からオプションではありません。

座標の基準点を変更して、プロット領域を基準にする方法はありますか?

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.position = c(0, 1), title="Legend placement makes me sad")

ここに画像の説明を入力

乾杯

4

4 に答える 4

73

更新:opts廃止されました。この回答でtheme説明されているように、代わりに使用してください。

kohske's answer を拡張するだけなので、次の人がそれに出くわすのはもう少し包括的です。

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
library(gridExtra)

a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(0, 1), legend.position = c(0, 1), title="Legend is top left")
b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(1, 0), legend.position = c(1, 0), title="Legend is bottom right")
c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(0, 0), legend.position = c(0, 0), title="Legend is bottom left")
d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(1, 1), legend.position = c(1, 1), title="Legend is top right")

grid.arrange(a,b,c,d)

ここに画像の説明を入力

于 2012-05-25T02:04:43.557 に答える
69

私は同様の答えを探していました。しかし、opts関数が ggplot2 パッケージの一部ではなくなっていることがわかりました。しばらく検索した後theme、 opts と同様のことができることがわかりました。したがって、このスレッドを編集して、他の人の時間を最小限に抑えます。

以下はnzcoopsによって書かれた同様のコードです。

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
library(gridExtra)

a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top left") + 
theme(legend.justification = c(0, 1), legend.position = c(0, 1))

b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom right") +
theme(legend.justification = c(1, 0), legend.position = c(1, 0))

c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom left") +
theme(legend.justification = c(0, 0), legend.position = c(0, 0))

d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top right") +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))

grid.arrange(a,b,c,d)

このコードは、まったく同様のプロットを提供します。

于 2016-02-04T06:06:07.423 に答える
56

更新:opts廃止されました。この回答でtheme説明されているように、代わりに使用してください。

デフォルトでは、ガイドの配置はプロット領域 (つまり、灰色で塗りつぶされた領域) に基づいていますが、位置合わせは中央に配置されます。したがって、左上の位置合わせを設定する必要があります。

ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
  opts(legend.position = c(0, 1), 
       legend.justification = c(0, 1), 
       legend.background = theme_rect(colour = NA, fill = "white"),
       title="Legend placement makes me happy")

ここに画像の説明を入力

デバイス領域全体に対してガイドを配置する場合は、gtable の出力を微調整できます。

p <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
  opts(legend.position = c(0, 1), 
       legend.justification = c(0, 1), 
       legend.background = theme_rect(colour = "black"),
       title="Legend placement makes me happy")

gt <- ggplot_gtable(ggplot_build(p))
nr <- max(gt$layout$b)
nc <- max(gt$layout$r)
gb <- which(gt$layout$name == "guide-box")
gt$layout[gb, 1:4] <- c(1, 1, nr, nc)
grid.newpage()
grid.draw(gt)

ここに画像の説明を入力

于 2012-05-25T01:35:49.887 に答える