2017-08-05 20 views
0

facet_gridで形成されたggplotグラフの間隔とラベルを制御しようとしています。私はいくつかの研究を行い、少なくとも最初の目的を達成するのに役立つだろうと思った議論を使用していますが、結果は私が期待したものではありません。facet_grid、ggplotのスペーシングとラベルの制御

再現可能な例として、私はmtcarsデータセット(ベースR)を使用して、コードの出力イメージを提供します。ここでは、変更したいものを示しています。

あなたのアドバイスは高く評価されます。

data(mtcars) 
setDT(mtcars) 
mtcars[, ":="(vs = as.factor(vs), am = as.factor(am), gear = as.factor(gear), carb = as.factor(carb))] 

ggplot (mtcars, 
     aes(x= disp , y = hp , colour = carb)) + 
    geom_point (size = 2) + facet_grid (gear ~ vs * am , margins = TRUE) + 
    xlab('disp') + ylab('hp') + 
    theme(panel.spacing.x=unit(2, "lines"), panel.spacing.y=unit(2, "lines"))+ 
    theme_economist() + theme(plot.margin = unit(c(1, 1, 1, 1), "lines")) 

enter image description here

答えて

0

あなたはthemeでパラメータpanel.spacingと定義することができ、パネルの間のスペース。 theme_economistがそれを変更しています。デフォルトでは、パネル間にスペースがあります。

各パネルのラベルに変数名を付けるために、ラベラー関数label_bothを追加することもできます。

ggplot (mtcars, 
     aes(x= disp , y = hp , colour = carb)) + 
    geom_point (size = 2) + facet_grid (gear ~ vs * am , margins = TRUE, labeller = label_both) + 
    xlab('disp') + ylab('hp') + 
    theme(panel.spacing.x=unit(2, "lines"), panel.spacing.y=unit(2, "lines"))+ 
    theme_economist() + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"), panel.spacing=unit(2,"lines")) 

enter image description here

関連する問題