簡體   English   中英

如何在ggplot2中更改boxplot圖例樣式

[英]How to change boxplot legend style in ggplot2

箱線圖例樣式在我的圖形中占用了太多空間。 我想知道ggplot2中的圖例樣式是否可以像基本軟件包中那樣進行更改。 我將當前的圖例樣式包含在ggplot2 http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/中,並且我想更改其他樣式,如鏈接http:/所示的線,框或圓/www.sthda.com/zh-CN/wiki/add-legends-to-plots-in-r-software-the-easiest-way 請查看頁面末尾的示例2。 謝謝。

因此,可以這樣做,但需要一些解決方法:繪制大小為-1的點(或其他幾何圖形)並使用可以實際編輯的圖例。 考慮例如

library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl), y = mpg, color = factor(cyl))) + 
  geom_point(size = -1, aes(fill = factor(cyl))) + 
  geom_boxplot(show.legend = FALSE) +
  scale_color_manual(name = "Number of Cylinders", values = c("blue", "red", "green")) +
  scale_fill_manual(name = "Number of Cylinders", values = c("blue", "red", "green")) +
  guides(colour = guide_legend(title.position = "top",
                               keywidth = unit(1, "cm"),
                               keyheight = unit(1, "cm"),
                               override.aes = list(shape = 22,
                                                   size = 10))) +
  theme(legend.position = c(0.14, 0.1),
        legend.direction = "horizontal") +
  labs(x = "Cylinders", y = "Miles/(US) gallon")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM