Alpha 不适用于 ggplot 散点图中的所有点

Alpha not applied for all points in ggplot scatterplot

我一直在尝试生成一个散点图,其中两个级别的 alpha 应用于高于或低于分数阈值的点。为此,我将每个点的 alpha 值存储在数据框中的向量 item_alpha 中,并在调用 geom_point:[=12= 时提供此向量作为 alpha 的参数]

library( ggplot2 );
library( scales );
one.data <- read.table("test.data", header = TRUE)

p1 <- ggplot( data = one.data )
p1 <- p1 + geom_point( aes( plot_X, plot_Y, colour = log10_p_value, size = plot_size, alpha = item_alpha ) )
p1 <- p1 + scale_colour_gradientn( colours = c("red", "yellow", "green", "blue"), limits = c( min(one.data$log10_p_value), max(one.data$log10_p_value)));
p1 <- p1 + geom_point( aes(plot_X, plot_Y, size = plot_size), shape = 21, fill = "transparent", colour = I (alpha ("black", 0.6) ));
p1 <- p1 + scale_size( range=c(5, 30)) + theme_bw();
one.x_range = max(one.data$plot_X) - min(one.data$plot_X);
one.y_range = max(one.data$plot_Y) - min(one.data$plot_Y);
p1 <- p1 + xlim(min(one.data$plot_X) one.x_range/10,max(one.data$plot_X)+one.x_range/10);
p1 <- p1 + ylim(min(one.data$plot_Y)one.y_range/10,max(one.data$plot_Y)+one.y_range/10);
p1

不过,似乎只为具有较小值的八个点正确设置了 alpha,而其余点仍然不透明。我查阅了 ggplot 文档,尝试了这些示例并尝试了一些其他变体,这些变体大多产生了各种错误,我真的希望有人能对此有所了解!提前致谢!

test.data 的内容:

"plot_X"    "plot_Y"    "plot_size" "log10_p_value" "item_alpha"
5.326   3.194   4.411   -27.3093    0.6
-2.148  7.469   3.434   -12.3487    0.6
-6.14   -2.796  3.062   -22.8069    0.6
3.648   6.091   3.597   -15.5032    0.6
0.356   -6.925  3.95    -10.4754    0.6
5.532   -0.135  3.246   -19.2883    0.6
3.794   -2.279  3.557   -16.4438    0.6
-3.784  1.42    2.914   -17.9687    0.6
-7.645  -1.571  3.163   -12.4498    0.6
-1.526  -4.756  3.509   -10.8972    0.6
-6.461  2.293   2.962   -13.4306    0.6
-5.806  0.983   4.38    -24.5422    0.6
-3.592  0.769   2.971   -17.8119    0.6
0.127   3.572   3.603   -11.4277    0.6
-0.566  0.706   3.77    -13.0952    0.3
2.25    -2.604  0.845   -11.7949    0.3
-7.845  -0.927  3.21    -12.6408    0.3
1.084   -6.691  3.654   -10.7319    0.3
-3.546  6.46    2.994   -11.6777    0.3
-5.478  -0.645  4.256   -17.7344    0.3
-6.251  -0.418  4.273   -19.29  0.3
-3.855  5.969   3.236   -10.9057    0.3
0.345   0.971   3.383   -11.5973    0.6
0.989   0.345   2.959   -10.8252    0.6

您正在使用 ggplot2 的明显基础绘图方法,这显然不是正确的方法。这里有两个选项:

dat <- read.table(text = "plot_X    plot_Y    plot_size log10_p_value item_alpha
5.326   3.194   4.411   -27.3093    0.6
-2.148  7.469   3.434   -12.3487    0.6
-6.14   -2.796  3.062   -22.8069    0.6
3.648   6.091   3.597   -15.5032    0.6
0.356   -6.925  3.95    -10.4754    0.6
5.532   -0.135  3.246   -19.2883    0.6
3.794   -2.279  3.557   -16.4438    0.6
-3.784  1.42    2.914   -17.9687    0.6
-7.645  -1.571  3.163   -12.4498    0.6
-1.526  -4.756  3.509   -10.8972    0.6
-6.461  2.293   2.962   -13.4306    0.6
-5.806  0.983   4.38    -24.5422    0.6
-3.592  0.769   2.971   -17.8119    0.6
0.127   3.572   3.603   -11.4277    0.6
-0.566  0.706   3.77    -13.0952    0.3
2.25    -2.604  0.845   -11.7949    0.3
-7.845  -0.927  3.21    -12.6408    0.3
1.084   -6.691  3.654   -10.7319    0.3
-3.546  6.46    2.994   -11.6777    0.3
-5.478  -0.645  4.256   -17.7344    0.3
-6.251  -0.418  4.273   -19.29  0.3
-3.855  5.969   3.236   -10.9057    0.3
0.345   0.971   3.383   -11.5973    0.6
0.989   0.345   2.959   -10.8252    0.6",header = TRUE)

dat$alpha_grp <- ifelse(dat$item_alpha == 0.6,'High','Low')

#If you want a legend; although you can suppress the legend
# here if you want.
ggplot(data = dat,aes(x = plot_X,y = plot_Y)) + 
    geom_point(aes(alpha = alpha_grp)) + 
    scale_alpha_manual(values = c(0.3,0.6))

#If you don't care about a legend
ggplot() + 
    geom_point(data = dat[dat$alpha_grp == 'High',],
                         aes(x = plot_X,y = plot_Y),alpha = 0.6) + 
    geom_point(data = dat[dat$alpha_grp == 'Low',],
                         aes(x = plot_X,y = plot_Y),alpha = 0.3)