2016-06-12 75 views
0

我想在標題(軸標題和繪圖標題)和繪圖邊緣之間創建空間。我試過axis.titleplot.title,但沒有運氣。當我嘗試vjust的各種值時,劇情沒有真正改變。我也試過plot.margin,但似乎也沒有發生。調整標題和繪圖邊緣之間的空白

數據:

data = data.frame(Category = c(0,1), value = c(40000, 120000)) 
data$Category = factor(data$Category, levels = c(0,1), labels = c("One-time", "Repeat")) 

簡介:

p = ggplot(data, aes(Category, Value)) + 
    geom_bar(stat = "identity", width = 0.5, position=position_dodge(width=0.9)) + 
    geom_text(aes(label=Value), position=position_dodge(width=0.9), family = "mono", vjust=-0.5) + 
    ggtitle("Title") + 
    scale_y_continuous(expand = c(0,0), limits = c(0,150000)) + 
    scale_x_discrete(expand = c(0,0), limits = c("One-time", "Repeat")) + 
    xlab("X axis Title") + 
    ylab("Y axis Title") 

主題:

p + theme(
    panel.grid.major = element_line(linetype = "blank"), 
    panel.grid.minor = element_line(linetype = "blank"), 
    axis.title = element_text(family = "sans", size = 15), 
    axis.text = element_text(family = "mono", size = 12), 
    plot.title = element_text(family = "sans", size = 18), 
    panel.background = element_rect(fill = NA) 
) 

Exported plot

回答

2

你要做到這一點使用margin

p + theme(
    panel.grid.major = element_line(linetype = "blank"), 
    panel.grid.minor = element_line(linetype = "blank"), 
    axis.title.x = element_text(family = "sans", size = 15, margin=margin(30,0,0,0)), 
    axis.title.y = element_text(family = "sans", size = 15, margin=margin(0,30,0,0)), 
    axis.text = element_text(family = "mono", size = 12), 
    plot.title = element_text(family = "sans", size = 18, margin=margin(0,0,30,0)), 
    panel.background = element_rect(fill = NA) 
) 

注意,保證金需要四個輸入和他們指定的順序頂部的空間,右,下,左。另外請注意我使用的是開發版的ggplot2版本,所以我的標題默認值是左對齊的。 'hjust'和'vjust'在ggplot2的舊版本中工作。

enter image description here

+0

嘿,那工作!似乎margin不能設置在更高級別的'axis.title'上,而是需要設置在各個軸級上('axis.title.x.'和'axis.title.y')。 – rnewbie