簡體   English   中英

為 scale_fill_continuous 連續比例而不是離散制作圖例

[英]Make legend for scale_fill_continuous continuous scale instead of discrete

我正在使用scale_fill_gradientn()的 function ,它通常應該產生一個連續的比例作為圖例,但是如果我使用下面附加的代碼,結果會產生一個帶有顏色框作為離散值的圖例

  ggplot(aes(x=grouping,y=person,fill=score)) + 
  geom_tile(stat = "identity",position = "identity",colour="black",size=1) + 
  facet_grid(family~functional,switch = "y") +
  scale_fill_gradientn(colours = c("white","orange","red"),
    limits=c(0,100),breaks = c(0,50,100),labels=c("min","mid","max"),
    guide = guide_legend(direction = "horizontal",title.position = "bottom")) +
    labs(fill = "Average Percentage")

在此處輸入圖像描述

我注意到,只要我從scale_fill_gradientn() function 中刪除guide參數,它就會給我一個連續的比例,即

scale_fill_gradientn(colours = c("white","orange","red"),
    limits=c(0,100),breaks = c(0,50,100),labels=c("min","mid","max"))

在此處輸入圖像描述

但是我的問題是為什么會發生這種情況,如果我必須避免使用guide參數,我該如何在刻度周圍添加黑色邊框(如上圖帶有離散顏色框的圖片)以及調整位置標題和標簽 例如,將圖例標題放在顏色條下方,將標簽放在顏色條上方? 非常感謝

這是一個潛在的解決方案,使用palmer penguins 數據集作為最小的、可重復的示例

library(tidyverse)
library(palmerpenguins)

penguins %>% 
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = bill_length_mm, fill = flipper_length_mm)) +
  geom_point(shape = 21, size = 3) +
  scale_fill_gradientn(colours = c("white", "orange", "red"),
                       limits = c(170, 240), breaks = c(180, 205, 235),
                       labels = c("min", "mid", "max"),
                       guide = guide_legend(direction = "horizontal",
                                            title.position = "bottom"))


penguins %>% 
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = bill_length_mm, fill = flipper_length_mm)) +
  geom_point(shape = 21, size = 3) +
  scale_fill_gradientn(colours = c("white", "orange", "red"),
                       limits = c(170, 240), breaks = c(180, 205, 235),
                       labels = c("min", "mid", "max"),
                       guide = guide_colourbar(direction = "horizontal",
                                               title.position = "bottom"))

代表 package (v2.0.1) 於 2022 年 7 月 25 日創建

這能解決你的問題嗎?

暫無
暫無

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

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