ggplot2:更改 scale_color_viridis_c 的 Alpha 但不更改图例

ggplot2: Change Alpha of scale_color_viridis_c but not legend

我想向我的 ggplot 添加一个 alpha,但不影响图例的 alpha。当前的解决方案是添加覆盖:guides(color = guide_legend(override.aes = list(alpha = 1)))。这适用于设置 alpha=1 但将图例比例更改为离散点而不是比例。

如何更改色阶的 alpha,同时保留图例中的完整可见性和实际比例?

示例代码:

library(ggplot2)

###Generate Mock Data ###
df<- data_frame(y=seq(1:100), x=seq(1:100), z=seq(1:100))

###Plot without Alpha ###
df %>% ggplot(aes(x=x, y=y, color=z)) +
  geom_point()+
  scale_color_viridis_c()

###Plot with Alpha showing alpha on legend with continuous scale ###
df %>% ggplot(aes(x=x, y=y, color=z)) +
      geom_point()+
      scale_color_viridis_c(alpha=0.01)

###Plot with Alpha showing alpha=1 on legend but scale changed to discrete###

df %>% ggplot(aes(x=x, y=y, color=z)) +
  geom_point()+
  scale_color_viridis_c(alpha=0.5)+
  guides(color = guide_legend(override.aes = list(alpha = 1)))

您只需将 alpha 添加到 geom_point() 而不是色阶。下面是一个可重现的示例,突出显示了您当前的方法与实现您所要求的正确方法之间的区别,即 'How can I change the alpha of my color scale while retaining full visibility and the actual scale in the legend?'

library(ggplot2)
library(vctrs)

###Generate Mock Data ###
df<- data_frame(y=seq(1:100), x=seq(1:100), z=seq(1:100))

###Plot with Alpha = 0 showing points and legend disappears###
ggplot(df,aes(x,y,color=z)) +
      geom_point()+
      scale_color_viridis_c(alpha=0.00)

###Plot with Alpha = 0.1 showing points and legend disappears###
ggplot(df,aes(x,y,color=z)) +
      geom_point()+
      scale_color_viridis_c(alpha=0.1)

###Plot with Alpha = 0 showing points disappear while legend remains visible###
ggplot(df,aes(x,y,color=z)) +
  geom_point(alpha=0.00)+
  scale_color_viridis_c()

###Plot with Alpha = 0 showing points disappear while legend remains visible###
ggplot(df,aes(x,y,color=z)) +
  geom_point(alpha=0.1)+
  scale_color_viridis_c()