I would like to make a scatter plot where every point gets a sphere. 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. 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:
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
. However, I can not figure out if and how to use this with colors that depend on the data.
TL;DR: How can I draw geom_points
that have a solid color and a transparent stroke but not constant colors?