Plot legend to move left ggplot

Viewed 1218

I like to move the legend a bit further on the left But I am not getting how to do it. Secondly, I also want to reduce the space between axis label and legend too

Can you please suggest something

the code is given below that I am using

The image of the graph is in the link below https://i.stack.imgur.com/GwvkR.jpg

"tooltip"

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 Answers

You should use legend.justification to get the legend on the left side of the plot and legend.margin to reduce the space between axis labels and the legend:

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

Does it answer your question ?

Related