2017-08-05 84 views
0

我想控制由facet_grid形成的ggplot圖中的間距和標籤。我做了一些研究,並且使用了我認爲能夠幫助我實現至少第一個目標的論據,但結果並不符合我的預期。控制facet_grid,ggplot中的間距和標籤

對於一個可重複使用的示例,我使用mtcars數據集(base 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

板之間的空間,您可以用參數panel.spacingtheme定義。 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