ggplot を使用して一部のデータをグラフ化していますが、凡例のテキストが非常に長く、ウィンドウに収まっていないことに気付きました。
+ opts(legend.position = 'bottom', legend.direction = 'horizontal', size=0.1) +
guides(colour = guide_legend(nrow = 3), size=1)
ウィンドウに合わせて凡例テキストをラップする ggplot のオプションはありますか。
私の知る限りではありませんのでstrwidth()、基本グラフィックスのテキストの幅を計算する を使用して回避策を講じました。
title <- "This is a really excessively wide title for a plot, especially since it probably won't fit"
par("din")デバイスの幅を取得しstrwidth()、テキスト サイズを計算するために使用します。
par("din")[1]
[1] 8.819444
strwidth(title, units="inches")
[1] 11.47222
関数とプロットで使用します。
wrapTitle <- function(x, width=par("din")[1]){
xx <- strwrap(x, width=0.8 * nchar(x) * width / strwidth(x, units="inches"))
paste(xx, collapse="\n")
}
wrapTitle(title)
[1] "This is a really excessively wide title for a plot, especially since it\nprobably won't fit, meaning we somehow have to wrap it"
プロット:
ggplot(mtcars, aes(wt, mpg)) + geom_point() + opts(title=wrapTitle(title))

par("din")プロットをファイルに保存する場合は、実際に保存されたプロットの寸法に置き換えることができます。