Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move legend outside the plot to the left at the bottom (in R)

I used the likert and ggplot2 package to create this graph. Now, I would like to move the legend at the bottom a little bit to the left, as the last part (Strongly Agree) is not shown in the graph. Unfortunately, I could not find a solution so far. How can I move/shift the legend to the left?

Link to the graph

The code:

plot(Likert_Uni_Study_Orientation_OF_V, low.color = "#007CC2", high.color = "#F7971C", neutral.color = "grey", neutral.color.ramp = "white", text.size=9) +
  theme(legend.text=element_text(size=24, margin = margin(r = 30, unit = "pt"))) + 
  theme(legend.direction = "horizontal", legend.position = "bottom") +
  theme(legend.title = element_blank()) +
  ggtitle("Uni Study Orientation – Only Fusha Learners") +
  theme(plot.title = element_text(hjust = 0.5, size =30)) + 
  theme(text = element_text(size = rel(6), color = "black"), axis.text.y = element_text(color = "black")) + 
  theme(axis.text.x = element_text(colour="black", size="30")) + 
  theme(axis.title.x = element_text(vjust=2, size=20, color = "black")) 

Thank you for your help!

like image 552
Steph Avatar asked Jan 01 '26 02:01

Steph


1 Answers

Specify a higher value for the width argument when saving to a file.

library(likert)

data(pisaitems)
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q']
names(items29) <- c("Magazines", "Comic books", "Fiction", 
                    "Non-fiction books", "Newspapers")
l29 <- likert(items29)

plot(l29)

enter image description here

Notice how the legend is clipped. Now send the plot to file and change the default value for width (for png it is 480). You may need a few trials until it appears just right.

png("l29.png", width=480*1.6)
plot(l29)
dev.off()

enter image description here

Another option is to place the legend on the right:

plot(l29, legend.position="right")
like image 157
Edward Avatar answered Jan 03 '26 20:01

Edward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!