gpt4 book ai didi

r - 调整标题和绘图边缘之间的空白

转载 作者:行者123 更新时间:2023-12-03 13:46:26 24 4
gpt4 key购买 nike

我想在标题(轴标题和图标题)和图的边缘之间创建空间。我尝试对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)
)

enter image description here

最佳答案

您想使用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)
)

请注意,margin需要四个输入,并且它们按上,右,下,左的顺序指定空格。另请注意,我使用的是ggplot2开发版,因此默认标题为左对齐。 'hjust'和'vjust'在ggplot2的旧版本中有效。

enter image description here

关于r - 调整标题和绘图边缘之间的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37776186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com