1

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!

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Steph
  • 13
  • 3
  • How will you present the graph? In a journal? Presentation in a lecture room? Online webpage? The width of the graphics device can be adjusted in various ways depending on the purpose. The obvious way is to increase the `width` argument of the various `grDevices` functions. `?Devices` – Edward Apr 16 '20 at 11:33
  • @Steph: can try this https://stackoverflow.com/questions/59482694/center-legend-in-ggplot2-relative-to-image – Tung Apr 16 '20 at 11:40
  • @ Edward: I will present it in my master thesis, that is, in a PDF file which will as well be printed. – Steph Apr 16 '20 at 17:47

3 Answers3

0

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")
Edward
  • 10,360
  • 2
  • 11
  • 26
  • Thank you very much. I was finally able to display my whole graph! The legend.position="right" command did not work though. – Steph Apr 16 '20 at 19:39
0

Addition to the first answer for those who are also new to R and need further explanation

These were my steps:

  1. I ran the code in R. The first line contains the changed width (and height). In the second line, I put the plot with all the other features. The third line contains dev.off(). This code saves the plot in .png format in a folder on the laptop.
png("Likert_Uni_Study_Orientation_OF_V.png", width=990*2, height=800)
plot("Likert_Uni_Study_Orientation_OF_V, low.color = "#007CC2", high.color = "#F7971C", neutral.color = "grey", neutral.color.ramp = "white", text.size=10) +
  theme(legend.text=element_text(size=28, 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 =40)) +
  theme(text = element_text(size = rel(7.5), 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"")
dev.off()
  1. I had to find out, where R saved my plot. For that, I only used the search function on my laptop. In my case, it was saved in "My PC" --> "Documents".
Steph
  • 13
  • 3
  • You can also modify the `res` argument to control the resolution of the plot. The default is 72 dpi, which is often too low for printing. Higher values increase the sharpness. Most journals require 300 minimum. – Edward Apr 16 '20 at 23:03
  • You can specify the location of the file in the `png` function. For example `png("D:/My Thesis/Plots/Likert.png", width=..., height=..., res=...)` – Edward Apr 16 '20 at 23:07
0

You can use cowplot as explained here Center legend in ggplot2 relative to image

(link given by @steph in the comments)

this one also works when the legend is much too big for ggplot to show it correctly at the bottom.

a= plot(l29)   
library (cowplot)

p1_legend <- get_legend(a+ theme(legend.position = 'right'))
plot_grid(a + theme(legend.position = 'none'), p1_legend,
      nrow = 2, rel_heights = c(1, 1))
Julien Colomb
  • 508
  • 4
  • 20