简体   繁体   English

将 plot 外的图例移到底部左侧(在 R 中)

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

I used the likert and ggplot2 package to create this graph.我使用 likert 和 ggplot2 package 创建此图。 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?如何将图例向左移动/移动?

链接到图表

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!谢谢您的帮助!

Specify a higher value for the width argument when saving to a file.保存到文件时为width参数指定更高的值。

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)

在此处输入图像描述

Notice how the legend is clipped.注意图例是如何被剪裁的。 Now send the plot to file and change the default value for width (for png it is 480).现在将 plot 发送到文件并更改width的默认值(对于 png,它是 480)。 You may need a few trials until it appears just right.您可能需要进行一些试验,直到它看起来恰到好处。

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

在此处输入图像描述

Another option is to place the legend on the right:另一种选择是将图例放在右侧:

plot(l29, legend.position="right")

Addition to the first answer for those who are also new to R and need further explanation除了第一个答案之外,那些也是 R 的新手并且需要进一步解释的人

These were my steps:这些是我的步骤:

  1. I ran the code in R.我在 R 中运行了代码。 The first line contains the changed width (and height).第一行包含更改后的宽度(和高度)。 In the second line, I put the plot with all the other features.在第二行中,我将 plot 与所有其他功能放在一起。 The third line contains dev.off().第三行包含 dev.off()。 This code saves the plot in.png format in a folder on the laptop.此代码将 plot in.png 格式保存在笔记本电脑的文件夹中。
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.我必须找出 R 保存了我的 plot 的位置。 For that, I only used the search function on my laptop.为此,我只在笔记本电脑上使用了搜索 function。 In my case, it was saved in "My PC" --> "Documents".就我而言,它保存在“我的电脑”->“文档”中。

You can use cowplot as explained here Center legend in ggplot2 relative to image您可以使用cowplot,如此处所述中心图例在ggplot2 相对于图像

(link given by @steph in the comments) (@steph 在评论中给出的链接)

this one also works when the legend is much too big for ggplot to show it correctly at the bottom.当图例太大而 ggplot 无法在底部正确显示时,这个也可以使用。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM