簡體   English   中英

將 plot 外的圖例移到底部左側(在 R 中)

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

我使用 likert 和 ggplot2 package 創建此圖。 現在,我想將底部的圖例向左移動一點,因為最后一部分(強烈同意)未顯示在圖表中。 不幸的是,到目前為止我找不到解決方案。 如何將圖例向左移動/移動?

鏈接到圖表

編碼:

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

謝謝您的幫助!

保存到文件時為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)

在此處輸入圖像描述

注意圖例是如何被剪裁的。 現在將 plot 發送到文件並更改width的默認值(對於 png,它是 480)。 您可能需要進行一些試驗,直到它看起來恰到好處。

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

在此處輸入圖像描述

另一種選擇是將圖例放在右側:

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

除了第一個答案之外,那些也是 R 的新手並且需要進一步解釋的人

這些是我的步驟:

  1. 我在 R 中運行了代碼。 第一行包含更改后的寬度(和高度)。 在第二行中,我將 plot 與所有其他功能放在一起。 第三行包含 dev.off()。 此代碼將 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. 我必須找出 R 保存了我的 plot 的位置。 為此,我只在筆記本電腦上使用了搜索 function。 就我而言,它保存在“我的電腦”->“文檔”中。

您可以使用cowplot,如此處所述中心圖例在ggplot2 相對於圖像

(@steph 在評論中給出的鏈接)

當圖例太大而 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