调整标题和绘图边缘之间的空白区域

时间:2016-06-12 15:50:25

标签: r ggplot2

我想在标题(轴标题和标题标题)和绘图边缘之间创建空格。我在axis.titleplot.title上尝试过vjust而没有运气。当我尝试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)
)

enter image description here

1 个答案:

答案 0 :(得分:3)

您希望使用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