图例向左移动ggplot

时间:2020-04-02 18:40:53

标签: r ggplot2 legend

我想将图例向左移动一点,但是我没有怎么做。 其次,我也想减少轴标签和图例之间的空间

您能建议点什么吗

下面是我正在使用的代码

该图的图像位于下面的链接中 https://i.stack.imgur.com/GwvkR.jpg

“工具提示”

ggplot(Q6_m, aes( choice,temp,fill=Answer ))+
  geom_bar(position = position_stack(reverse = TRUE), stat="identity") +
  coord_flip() +
  xlab("") +
  ylab("Number of responses") +
  scale_fill_brewer(type = "div") +
  theme(axis.text=element_text(size=8),
        axis.title=element_text(size=8,face="bold"), legend.title = element_blank(),
        legend.text=element_text(size=7)) +
  ggtitle("Q6:Rate your ability to perform the following procedures WITHOUT attending assistance?")+
  theme(plot.title = element_text(color = "black", size = 7.5, face = "bold", hjust = 1))+
  facet_wrap(~gender,scales = "free_x")+
  theme(legend.position="bottom", legend.direction  = "horizontal",legend.key.size = unit(0.5,"line")
        )

1 个答案:

答案 0 :(得分:1)

您应该使用legend.justification来使图例位于图的左侧,并使用legend.margin来减小轴标签和图例之间的间距:

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_boxplot()+
  theme(legend.position = "bottom",
        legend.justification = c(0,1),
        legend.margin = margin(t = -15, r = 0, b = 0, l = 0, unit = "pt"))

enter image description here

它回答了您的问题吗?