Im trying to use a scale to create points with colors of different alpha
My data.frames:
df2$membership
[1] 0.34185026 0.15610992 0.04991413 0.00000000 0.52687792 0.16631913 0.11915588 0.26531666 0.16631913 0.34312613 0.39910748 0.00000000
[13] 0.31770686 0.37701944 0.19777656 0.00000000 0.28141323 0.38329395 0.00000000 0.45098905 0.15583159 0.00000000 0.15610992 0.00000000
[25] 0.45297710 0.38409023 0.38329395 0.06272793 0.28141323
df$cluster+1
[1] 3 2 3 1 3 3 3 3 3 3 3 3 3 3 3 2 3 3 1 3 3 1 2 1 3 3 3 2 3
df
V1 V2
1 -60.950322 3.268223
2 120.989186 -18.171861
3 -100.891471 -54.566570
4 123.853486 -31.076373
5 47.068591 21.517879
6 -96.289311 -32.327825
7 -55.133477 -23.899109
etc
I did it with the following code with plot()
plot(df, col=df2$cluster+1, pch=21, cex = 2)
colors <- mapply(function(col, i) adjustcolor(col, alpha.f = df2$membership_prob[i]),
palette()[df2$cluster+1], seq_along(df2$cluster))
points(df, col=colors, pch=20, cex = 2) +
text(df, labels = rownames(df), pos = 4)
Which gives me:
But I'm having problems with doing this in ggplot2, because I already do all my other graphs in ggplot2 and aesthetics are very different.
What I need is a way to use a continous scale (df2$membership_prob) as a alpha level for my pallete which is df2$cluster+1.
Reproducible example of what I need with ggplot (alpha levels within color):
library("dbscan")
data("moons")
cl <- hdbscan(moons, minPts = 5)
plot(moons, col=cl$cluster+1, pch=21)
colors <- mapply(function(col, i) adjustcolor(col, alpha.f = cl$membership_prob[i]),
palette()[cl$cluster+1], seq_along(cl$cluster))
points(moons, col=colors, pch=20)