简体   繁体   English

为 scale_fill_continuous 连续比例而不是离散制作图例

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

I'm using the function of scale_fill_gradientn() which normally should produce a continuous scale as the legend but somehow if I use the following code attached below the outcome produces a legend with boxes of colours as discrete values我正在使用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")

在此处输入图像描述

I notice that as long as I remove the guide argument from the scale_fill_gradientn() function it would give me back a continuous scale ie我注意到,只要我从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"))

在此处输入图像描述

But then my question would be why does this occur and if I have to refrain from using the guide argument how can I add a black border around the scale (like the picture above with the discrete colour boxes) as well as adjust the positioning of the title and labels eg put the legend title below the colour bar and labels above the colour bar?但是我的问题是为什么会发生这种情况,如果我必须避免使用guide参数,我该如何在刻度周围添加黑色边框(如上图带有离散颜色框的图片)以及调整位置标题和标签 例如,将图例标题放在颜色条下方,将标签放在颜色条上方? Thanks a lot非常感谢

Here is a potential solution using the palmer penguins dataset as a minimal, reproducible example :这是一个潜在的解决方案,使用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"))

Created on 2022-07-25 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 7 月 25 日创建

Does this solve your problem?这能解决你的问题吗?

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

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