如何将图例移近ggplot2中的x轴标签

use*_*004 4 r ggplot2

我正在创建一个函数来创建哑铃图,图例位于底部。但是,它距离 x 轴标题太远。我想将其稍微向上移动,使其位于 x 轴下方 10 像素处。

这是代码:

vertical_theme = theme_bw(base_family = "Georgia") +
  theme(
    panel.border = element_rect(color = "black", fill=NA),
    axis.title.x = element_text(hjust=0.5, size = 10, margin=margin(t=10, b=10)),
    axis.text.y = element_text(size=10, margin=margin(r=10), color="black", hjust=0),
    axis.text.x = element_text(size=10, margin=margin(t=10), color="black"),
    axis.title.y = element_blank(),
    legend.title = element_blank(),
    legend.position= "bottom",
    legend.text = element_text(size = 10, margin = margin(r = 10)),
    panel.grid.major.y = element_blank() ,
    panel.grid.minor.y = element_blank(),
    panel.grid.major.x = element_line(size=1), 
    panel.grid.minor.x = element_blank(),
    plot.margin = margin(10, 30, 10, 10, "pt"))

dumbbell = function(df) {
  ggplot(df, aes(pct_responses, Domain)) +
    geom_line(aes(group=Domain)) +
    geom_point(aes(shape=race), size=5, color="#3bbae0" ) +
    vertical_theme + 
    scale_shape_manual(labels = c("Black Students", "White Students"), 
                       values=c(15, 19)) +
    scale_x_continuous(expand = c(0, 0), 
                       limits=c(0,100), 
                       breaks = seq(0, 100, by=20),
                       labels = function(x) paste0(x,"%")) +
    labs(x = "% of Responses") +
    scale_y_discrete(labels = wrap_format(40))
}

dumbbell(df)

Run Code Online (Sandbox Code Playgroud)

这是一个屏幕截图(y 轴上的标签已删除,因为该数据尚未公开):

在此输入图像描述

我尝试使用 legend.position = c(0.5, 0) 手动调整 legend.position (使用各种不同的数字),但图例与“响应的百分比”重叠。

Ken*_*son 8

用于theme(legend.margin=margin(-10, 0, 0, 0))向上移动图例。-10根据需要进行调整。