Reputation: 1234
I am trying to create an NMDS plot using ggplot. In addition to the standard plotting of points (communities/assemblages) and hulls etc. I would also like to adjust the alpha of each point.
I have calculated a vector of numeric values from 0-1 (called "Alpha") which reflects the timing of the surveys which correspond to each point and I would like to use this to modify the alpha of each point. Thus far I have been using variations on the theme of:
ggplot() +
geom_point(data = data.scores, aes(x = NMDS1, y = NMDS2, colour =Treatment), size = 3) +
scale_colour_manual(values = c("Burnt" = "black", "Grazed" = "tan4","Control" = "green4")) +
scale_alpha_manual(values = Alpha) +
geom_polygon(data = hull.data, aes( x = NMDS1, y = NMDS2, colour = Treatment),
linetype = "dashed", fill = NA) +
coord_equal() +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
legend.position="top")
This has only got me plots where the alpha of all the points is 1, the alpha is not 1 but is set to the same value (presumably the first value in my vector) or error messages. Does anyone have any idea what I'm doing wrong?
For reference I was able to create what I want using base R graphics fairly easily but I need my plot to be a ggplot graphic:
Upvotes: 1
Views: 1535
Reputation: 10671
ggplot()
syntax expects the values being mapped for a layer to be passed in that layer, so in geom_point()
, and not by adding a scale. So right now you are not defining which layers you want the alpha scale to be applied to, thus all are being shown as alpha = 1
(the default).
You can just use the raw vector directly in geom_point()
. Using a reproducible example with mtcars
:
library(ggplot2)
set.seed(1)
# sim your Alpha
Alpha <- runif(nrow(mtcars))
ggplot(data = mtcars, aes(mpg, hp)) +
geom_point(alpha = Alpha)
scale_alpha_manual()
won't have any effect on this plot since you are not mapping anything with aes(alpha = ...)
.
Upvotes: 3