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.
我正在尝试使用ggplot创建NMDS图。除了点(社区/组合)和船体等的标准绘图之外,我还想调整每个点的alpha。
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:
我已经计算了一个0-1的数值向量(称为“Alpha”),它反映了与每个点相对应的调查的时间,我想用它来修改每个点的alpha。到目前为止,我一直在使用以下主题的变体:
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?
这只得到了我所有点的alpha为1,alpha不是1但是设置为相同值(可能是我向量中的第一个值)或错误消息的图。有谁知道我做错了什么?
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:
作为参考,我能够相当容易地使用基本R图形创建我想要的东西,但我需要我的情节是ggplot图形:
1 个解决方案
#1
1
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).
ggplot()语法期望为图层映射的值在该图层中传递,因此在geom_point()中,而不是通过添加比例。所以现在你没有定义你想要应用alpha刻度的图层,因此所有图层都显示为alpha = 1(默认值)。
You can just use the raw vector directly in geom_point()
. Using a reproducible example with mtcars
:
您可以直接在geom_point()中使用原始向量。使用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 = ...)
.
scale_alpha_manual()不会对此绘图产生任何影响,因为您没有使用aes(alpha = ...)映射任何内容。
#1
1
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).
ggplot()语法期望为图层映射的值在该图层中传递,因此在geom_point()中,而不是通过添加比例。所以现在你没有定义你想要应用alpha刻度的图层,因此所有图层都显示为alpha = 1(默认值)。
You can just use the raw vector directly in geom_point()
. Using a reproducible example with mtcars
:
您可以直接在geom_point()中使用原始向量。使用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 = ...)
.
scale_alpha_manual()不会对此绘图产生任何影响,因为您没有使用aes(alpha = ...)映射任何内容。