简体   繁体   English

如何在 geom_jitter 中为比例指南设置不同的填充颜色?

[英]How to set different fill colour for scale guide in geom_jitter?

I want to create a plot where the geom_points (or jitters) are filled after a given value, but their line color is white.我想创建一个 plot,其中 geom_points(或抖动)在给定值之后填充,但它们的线条颜色为白色。

However, setting colour to white makes it so the legend shows invisible points, and is rather useless.但是,将颜色设置为白色会使图例显示不可见的点,并且相当无用。 How can I force the legend to display black points with the correct size, but the actual plot has different colour and fill?如何强制图例以正确的大小显示黑点,但实际的 plot 具有不同的颜色和填充?

An example of what I have gotten to work:我已经开始工作的一个例子:

mtcars %>% 
  ggplot(aes(disp, mpg, size = cyl, fill = mpg)) +
  geom_jitter(alpha = 0.8, pch = 21) +
  scale_size_continuous(trans = 'log10') +
  scale_fill_viridis_c()

How can i set color = 'white' inside geom_jitter , without creating a useless legend?如何在geom_jitter中设置color = 'white' ,而不创建无用的图例?

在此处输入图像描述

You can override specific aesthetics in the legend of a scale by using guide_legend(override.aes = list(...)) .您可以使用guide_legend(override.aes = list(...))比例图例中的特定美学。 Example below:下面的例子:

library(ggplot2)

ggplot(mtcars, aes(disp, mpg, size = cyl, fill = mpg)) +
  geom_jitter(alpha = 0.8, pch = 21, colour = "white") +
  scale_size_continuous(
    trans = 'log10',
    guide = guide_legend(override.aes = list(fill = "black"))
  ) +
  scale_fill_viridis_c()

Created on 2021-05-26 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 5 月 26 日创建

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

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