简体   繁体   English

R ggplot2:如何绘制具有纯色和透明笔划并根据颜色着色的geom_points?

[英]R ggplot2: How to draw geom_points that have a solid color and a transparent stroke and are colored depending on color?

I would like to make a scatter plot where every point gets a sphere.我想做一个散点图 plot 每个点都有一个球体。 Both the dot and its sphere are colored according to some column values.点及其球体都根据某些列值着色。

A minimal example that shows what I want:一个显示我想要的最小示例:

library(ggplot2)
library(vcd) # only needed for example dataset
ggplot(Arthritis, aes(x = ID, y = Age)) + 
    geom_point(aes(color=Sex), size=10, alpha=.3) + 
    geom_point(aes(color=Treatment), size=3)

在此处输入图像描述

The problem with this "solution" is that using two geom_point layers seems to mess up the legend.这个“解决方案”的问题在于,使用两个geom_point图层似乎搞砸了传说。 I guess it would also make much more sense to only have one geom_point layer and use a shape that also adds a stroke, so something like this:我想只有一个geom_point图层并使用还添加笔画的形状也会更有意义,所以像这样:

ggplot(Arthritis, aes(x = ID, y = Age)) + 
    geom_point(aes(color=Sex, fill=Treatment), shape=21, size=5, stroke=5)

在此处输入图像描述 Here the legend makes way more sense, however, I can not figure out how to make the stroke transparent.这里的传说更有意义,但是,我不知道如何使笔触透明。 This is important because you just can not see anything anymore when points overlap.这很重要,因为当点重叠时您将看不到任何东西。

Answers like this do not solve my problem, because they use a constant color and thus can use the function alpha .这样的答案不能解决我的问题,因为它们使用恒定的颜色,因此可以使用 function alpha However, I can not figure out if and how to use this with colors that depend on the data.但是,我不知道是否以及如何将它与依赖于数据的 colors 一起使用。

TL;DR: How can I draw geom_points that have a solid color and a transparent stroke but not constant colors? TL;DR:如何绘制具有纯色和透明笔划但不是恒定geom_points的 geom_points?

You are on the right track to recognize that you can use the function alpha() , and have realized that you cannot just put alpha() within aes() .您在正确的轨道上认识到您可以使用 function alpha() ,并且已经意识到您不能只将alpha()放在aes()中。 You can, however, pass alpha() as the values= argument within any scale_* functions.但是,您可以在任何scale_*函数中将alpha()作为values=参数传递。 Here's an example using mtcars :这是使用mtcars的示例:

ggplot(mtcars, aes(mpg, disp)) + 
  geom_point(
    aes(color=factor(cyl), fill=factor(carb)),
    shape=21, size=4, stroke=4) +
  scale_color_manual(values=alpha(rainbow(3), 0.2))

在此处输入图像描述

One problem with that is those big black lines around the " factor(carb) legend don't sit well with me. Super ew. You can get rid of them using the guides() function and using override.aes= to specify what you want shown there and what to replace it with. In this case, you can set the color=NA to override the inherited aesthetic to be transparent (leaving only the fill= part).一个问题是“ factor(carb)图例周围的那些大黑线不适合我。超级ew。你可以使用guides() function并使用override.aes=来指定你的内容想要在那里显示以及用什么替换它。在这种情况下,您可以设置color=NA以覆盖继承的审美为透明(仅保留fill=部分)。

ggplot(mtcars, aes(mpg, disp)) + 
  geom_point(
    aes(color=factor(cyl), fill=factor(carb)),
    shape=21, size=4, stroke=4) +
  scale_color_manual(values=alpha(rainbow(3), 0.2)) +
  guides(fill=guide_legend(override.aes = list(color=NA))) +
  labs(color="cyl", fill="carb")

在此处输入图像描述

BTW, there's no simple way to place the stroke "behind" the fill part for geom_point .顺便说一句,没有简单的方法可以将笔画“放在” geom_point的填充部分“后面”。 You can probably write your own custom stat/geom for doing that, but geom_point is always drawn with fill first, then stroke.您可能可以为此编写自己的自定义 stat/geom,但geom_point总是先用填充绘制,然后是描边。

A simple way round this is to make it so that the larger transparent circles aren't points at all, but filled circles.解决此问题的一种简单方法是使较大的透明圆圈根本不是点,而是实心圆圈。 That way you can use the fill aesthetic to label them.这样你就可以使用fill美学来 label 他们。 This uses geom_circle from ggforce :这使用来自geom_circleggforce

library(ggplot2)
library(vcd)
library(ggforce)

ggplot(Arthritis) + 
  geom_circle(aes(x0 = ID, y0 = Age, r = 2, fill = Sex), alpha = .3, colour = NA) +
  geom_point(aes(x = ID, y = Age, color = Treatment), size = 3) + 
  coord_equal() + 
  scale_color_discrete(h = c(350, 190))

Created on 2020-07-01 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 7 月 1 日创建

Or make a second color scale!或者制作第二个色标!

library(ggplot2)
library(vcd) 
#> Loading required package: grid
library(ggnewscale)

ggplot(Arthritis, aes(x = ID, y = Age)) + 
  ## removing stroke so it does not have this awkward border around it
  geom_point(aes(color=Sex), size=10, alpha=.3, stroke = 0) +
  new_scale_color()+
  geom_point(aes(color=Treatment), size=3) 

Created on 2022-06-15 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 6 月 15 日创建

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

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