Ns

R ggplot2-下部の凡例が切り取られます。凡例に最適な列数をその場で見つける方法は?



R Ggplot2 Legend Bottom Gets Cut



解決:

これはうまくいきました:

  • フォントサイズを小さくします(width_scale * 1.5ではなくwidth_scale)
  • 凡例の周りにボックスを追加します
ggplot2 :: ggplot(mydf、ggplot2 :: aes(group、value、fill = var))+ ggplot2 :: geom_bar(stat = 'identity')+ ggplot2 :: scale_y_continuous( '%')+ ggplot2 :: theme_light() + ggplot2 :: theme(legend.text = ggplot2 :: element_text(size = width_scale)、legend.box.margin = margin(6、6、6、6)、legend.title = ggplot2 :: element_text(size = 1.5 * width_scale、face = 'bold')、legend.position = 'bottom'、legend.key.size = grid :: unit(width_scale / 50、 'inch')、legend.key.width = grid :: unit(width_scale / 50、 'インチ'))

結果を見る



凡例の要素数に応じてwidth_scaleを調整できます。

それは<- data.frame(group=paste0('gr',1:10), var=paste('some long text -', 1:50), value=runif(500, 0, 100)) width_scale <- 12 * 26 / length(unique(mydf$var))  

width_scale調整




あなたは調整することができますグラフィックが要件に一致するまでサイズ値を変更して、手動でサイズを設定しますか?

print(#or ggsave()ggplot2 :: ggplot(mydf、ggplot2 :: aes(group、value、fill = var))+ ggplot2 :: geom_bar(stat = 'identity')+ ggplot2 :: scale_y_continuous( '%') + ggplot2 :: theme_light()+ ggplot2 :: theme(legend.text = ggplot2 :: element_text(size = 14)、legend.position = 'bottom')、legend.title = ggplot2 :: element_text(size = 1.5 * width_scale 、face = 'bold')、legend.key.size = grid :: unit(width_scale / 50、 'inch')、legend.key.width = grid :: unit(width_scale / 50、 'inch')))

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