我想在标题(轴标题和情节标题)和情节边缘之间创建空间。我试图vjust上axis.title并plot.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"))
Run Code Online (Sandbox Code Playgroud)
阴谋:
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")
Run Code Online (Sandbox Code Playgroud)
主题:
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)
)
Run Code Online (Sandbox Code Playgroud)
jal*_*pic 14
你想使用 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)
)
Run Code Online (Sandbox Code Playgroud)
请注意,边距需要四个输入,它们按顶部、右侧、底部、左侧的顺序指定空间。另请注意,我使用的是开发 ggplot2 版本,因此我的标题默认值是左对齐的。'hjust' 和 'vjust' 在旧版本的 ggplot2 中工作。
您还可以为 margin 参数赋予“-ve”值,以使标题更接近情节。
gg_1 +
theme(plot.title = element_text(size = 12,
hjust = 0.5,
family = fam_3,
face = "bold",
margin = margin(0,0,-10,0))
Run Code Online (Sandbox Code Playgroud)