-1

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:

Points with differing alpha levels

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)
Gabriel G.
  • 555
  • 1
  • 3
  • 13
  • 2
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. Right now it's unclear what difficulty you're having in assigning variables to the alpha and/or color. – camille Apr 25 '19 at 19:01
  • It's difficult without a reproducible example but you can probably do it using `scale_alpha` https://ggplot2.tidyverse.org/reference/scale_alpha.html – Matias Andina Apr 25 '19 at 19:04
  • I'm sorry camille, I'm new to stack.overflow and often I'm not sure what data I need to present so you can reproduce the code I generated. – Gabriel G. Apr 25 '19 at 19:16
  • Best practice for sharing data for examples is to provide the output of `dput(head(YOUR_DATA))`, where YOUR_DATA includes the columns you're working with. Then we can easily recreate your exact data frame in one step. – Jon Spring Apr 25 '19 at 19:16
  • `library(ggplot2); ggplot(df, aes(V1, V2, alpha = membership_prob, color = 'cluster+1')) + geom_point(shape = 20, size = 2)` – Jon Spring Apr 25 '19 at 19:29
  • So you said you're having trouble doing this with `ggplot`—what have you tried? I'm just not sure what the problem is with assigning color or alpha – camille Apr 25 '19 at 19:37
  • Jon, this one did work. But the problem now is that there is not border around points, and labels are also affected by alpha! As you can see in the example, the plot first plots the outside of points, and then the fill with alpha included (which is why I have no clue as how to do it) – Gabriel G. Apr 25 '19 at 19:44

2 Answers2

1
library(tidyverse)
moons %>% 
  bind_cols(membership_prob = cl$membership_prob) %>%
  bind_cols(cluster = as.factor(cl$cluster)) %>%  # to make discrete var
  ggplot(aes(X,Y, color = cluster, alpha = membership_prob)) +
  geom_point(size = 5, shape = 20)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

After reading Jon Spring answer, i've come with a solution within ggplot which does not affect the labels alpha:

   library(ggplot2); 
   ggplot(df, aes_string("V1", "V2", color = "cluster+1")) + 
   geom_point(shape = 21, size = 4,fill = NA) +  #solid points with no fill
   geom_point(shape = 20, size = 3, aes_string(x="V1", y="V2", color="cluster+1", alpha = "a$V1")) #fill with alpha values
Gabriel G.
  • 555
  • 1
  • 3
  • 13