ggplot: Adding alpha value to a whole layer - r

I am trying to plot some boxplots as semi-transparent. When I set the alpha value, only the fill transparency is adjusted, and not the borders/strokes/colors.
Any idea how to make the whole geom layer transparent?
library(ggplot2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(cyl), color = factor(cyl)), alpha = 0)

This won't work out of the box, since for polygons ggplot2 only applies alpha to fill, not colour. In order to fix that, we'll apply the following ad-hoc patch by taking low-level internals and adding alpha mapping where needed.
Check out the following gist. Won't post it here, as it is too lengthy.
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(cyl), color = factor(cyl)), alpha = 0.4, size = 1.4)
Before:
After:

Related

Subdividing jitter points in ggplot

I'm having difficulty getting geom_jitter to separate my data points by a third variable (phase).
I'd like for each bar in this 2x2 graph to have its own set of jittered data points for each phase condition.
Right now the points are grouped between the two phase bars (sorry couldn't upload the image -- don't have my 10 posts yet).
I know I'm missing something in the geom_jitter aes, but not sure what it is -- fill=phase is not doing the trick.
ggplot(datalong, aes(grp, score)) +
geom_bar(stat = "summary", fun.y = "mean", aes(fill=phase), position = "dodge", alpha = .5) +
geom_jitter(inherit.aes = FALSE, aes(grp, score, fill=phase), position = position_jitter(0.2))
The default shape for points does not take the fill aesthetic
Compare
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, fill = gear)) +
geom_jitter()
ggplot(mtcars, aes(mpg, disp, color = gear)) +
geom_jitter()
ggplot(mtcars, aes(mpg, disp, fill = gear)) +
geom_jitter(shape = 21)
Created on 2020-07-09 by the reprex package (v0.3.0)
Found a solution using:
geom_point(position=position_jitterdodge())
This seems to jitter and dodge to separate the 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. 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?
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(). You can, however, pass alpha() as the values= argument within any scale_* functions. Here's an example using 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).
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. You can probably write your own custom stat/geom for doing that, but geom_point is always drawn with fill first, then stroke.
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. This uses geom_circle from ggforce:
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)
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)

ggplot2 2.0.0 coloured boxplots and jitter with borders

I am trying to make a boxplot filled by a binary variable, with a facet grid. I also want to have jitter on top of the boxplots, but without getting them confused with the outliers. In order to fix this, I have added colour to the jitter, but by doing so, they meld in with the already coloured boxplots, as they are the same colour.
I really want to keep the colours the same, so is there a way to add borders to the jitter (or is there a different way to fix the outlier problem)?
Example code:
plot <- ggplot(mpg, aes(class, hwy))+
geom_boxplot(aes(fill = drv))+
geom_jitter(width = .3, aes(colour =drv))
# facet_grid(. ~some_binary_variable, scales="free")
You can use a filled plotting symbol (21:25, cf. ?pch) and then use a white border to differentiate the points:
ggplot(mpg, aes(class, hwy))+
geom_boxplot(aes(fill = drv))+
geom_jitter(width = .3, aes(fill = drv), shape = 21, color = "white")

Overlaying jittered points on boxplot conditioned by a factor using ggplot2

I am making a boxplot conditioned by a factor similar to this example:
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(aes(fill = factor(am)))
There are few points in the data set, and I'd like to express this visually by overlaying the data points. I want to overlay the points colored by the same factor "am" which I try to do like this:
p + geom_boxplot(aes(fill = factor(am))) + geom_jitter(aes(colour = factor(am)))
The points are colored by the factor "am" but not spaced to lay only over the box plots they are associated with. Rather they mix and cover both.
Does anyone know how the condition the geom_jitter so the points associate with the factor "am"?
Welcome to SO! Here's my attempt. It's a bit clumsy, but does the job. The trick is to map x to a dummy variable with manually constructed offset. I'm adding a fill scale to highlight point positioning.
mtcars$cylpt <- as.numeric(factor(mtcars$cyl)) + ifelse(mtcars$am == 0, -0.2, 0.2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(am))) +
geom_point(aes(x = cylpt, colour = factor(am)), position = "jitter") +
scale_fill_manual(values = c("white", "gray"))
I have found this link that solves your problem:
https://datavizpyr.com/how-to-make-grouped-boxplot-with-jittered-data-points-in-ggplot2/
geom_jitter(position = position_jitterdodge())

How to fill boxes in geom_point legend with color of points, not just increasing their size?

I'm having a similar problem as described in here under "2- After having the two legends...", but instead of increasing the point size (which eventually also enlarges the legend itself), I would like fill each box in the legend with the corresponding color. Like in a bar plot's legend. Data & code examples here.
Looking through several other questions here, the ggplot docu, etc., I tried variations of code-snippets I found, but couldn't figure out a solution. The legend always retained the point symbols.
Therefore: If possible, how to tweak or replace the legend of a point/scatter/bubble plot so that it looks like the legend of a bar plot? Or, more generally, how to replace the legend of a given geom in ggplot2 with that of a different one? Thank you for any hints!
Edit: Example with mtcars data
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl), size = qsec))
p
Adding what I gathered from other SO-answers...
p <- p + guides(colour = guide_legend(override.aes = list(fill = unique(mtcars$cyl))))
p
...keeps the points, instead of expanding the color to fill the legend box, no matter arguments and datasources I try for guides() and list().
On the other hand:
ggplot(mtcars, aes(wt, mpg)) + geom_bar(aes(fill = factor(cyl)), stat="identity")
...draws nicely color-filled boxes to the legend. That's what I'm trying to do for a bubble plot.
You won't be able to get a fill-type legend per se, but you can easily emulate it:
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl), size = qsec)) +
guides(col = guide_legend(override.aes = list(shape = 15, size = 10)))

Resources