【问题标题】:ggplot XY scatter - how to change alpha transparency for select points?ggplot XY scatter - 如何更改选择点的 alpha 透明度?
【发布时间】:2015-09-06 11:43:19
【问题描述】:

我有大约 14,000 个 XY 对要绘制,并为此使用 ggplot2

由于分数很高,我不得不使用非常低的alpha=0.025。 我想用不同的颜色突出显示 7 个 XY 点,并且更不透明,并附带一个文本图例。

目前,我的 7 个特殊数据点的颜色没有显示,因为它们也在 alpha=0.025。如何仅增加这些点的不透明度?

我目前的语法是:

trial <- ggplot(df, aes(x = df$SeqIdentityMean, 
                        y = df$SeqIdentityStdDev,
                        color = factor(df$PfamA_ID))) + 
            geom_point(alpha=0.025) + 
            labs(title="Mean Vs. standard deviation of PfamA seed lengths", 
                 x="Average Length (aa)",
                 y="Standard  Deviation of Length (aa)") + 
            theme(legend.title=element_blank(),
                  legend.key=element_rect(fill='NA'))

【问题讨论】:

    标签: r ggplot2 alpha scatter


    【解决方案1】:

    我们可以使用annotate:

    ggplot(df, aes(x=SeqIdentityMean,
                   y=SeqIdentityStdDev,
                   color=PfamA_ID))+
      geom_point(alpha=0.05) +
      annotate("point",
               df$SeqIdentityMean[special.points],
               df$SeqIdentityStdDev[special.points])
    

    使用@jlhoward 的示例数据:

    ## create artificial data set for this example
    set.seed(1)     # for reproducibility
    n  <- 1.4e4     # 14,000 points
    df <- data.frame(SeqIdentityMean  =rnorm(n, mean=rep(-3:3, each=n/7)), 
                     SeqIdentityStdDev=rnorm(n, mean=rep(-3:3, each=n/7)),
                     PfamA_ID=rep(1:7, each=n/7))
    df$PfamA_ID <- factor(df$PfamA_ID)
    
    ## you start here
    library(ggplot2)
    special.points <- sample(1:n, 7)
    

    编辑 1: 我们可以加annotate("text",...)

    ggplot(df, aes(x=SeqIdentityMean,
                   y=SeqIdentityStdDev)) +
      geom_point(alpha=0.05) +
      annotate("point",
               df$SeqIdentityMean[special.points],
               df$SeqIdentityStdDev[special.points],
               col="red") +
      annotate("text",
               df$SeqIdentityMean[special.points],
               df$SeqIdentityStdDev[special.points],
               #text we want to display
               label=round(df$SeqIdentityStdDev[special.points],1),
               #adjust horizontal position of text
               hjust=-0.1)
    


    编辑 2:

    #subset of special points
    df_sp <- df[special.points,]
    
    #plot
    ggplot(df, aes(x=SeqIdentityMean,
                   y=SeqIdentityStdDev)) +
      geom_point(alpha=0.05) +
      #special points
      geom_point(data=df_sp,
                 aes(SeqIdentityMean,SeqIdentityStdDev,col=PfamA_ID),size=3) +
      #custom legend
      scale_colour_manual(name = "Special Points",
                          values = df_sp$PfamA_ID,
                          labels = df_sp$SeqIdentityMean)
    

    【讨论】:

    • 如何为突出显示的点着色,用黑色散布(与您显示的相反),并在另一列输入中添加文本,仅用于图例中的 special.points?谢谢!
    • 哎呀,我之前的请求并不清楚。让我澄清一下。如何使这些带注释的颜色各不相同,然后在绘图之外有一个带有颜色和相应文本的图例(来自输入数据列#1,但仅针对 special.points)。所以我不希望它们都是红色的,并且对于情节内部的文本,但在情节之外。很抱歉很痛苦,但对 R 来说是新手,对 ggplot 来说是全新的 :)
    【解决方案2】:

    只需在数据集中创建一个 alpha 列,然后将要突出的点设置为 alpha = 1

    library(ggplot2)
    alpha_vector = rep(0.025, nrow(mtcars))
    alpha_vector[c(3,6,8)] = 1
    mtcars$alpha = alpha_vector
    ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(aes(alpha = alpha))
    

    这里的诀窍是要意识到 alpha 只是另一种美学。

    此外,我不会直接绘制 14k 点并依赖 alpha,我只会使用 2d 分箱。例如使用 hexbin:

    ggplot(mtcars, aes(x = wt, y = mpg)) + geom_hexbin()
    

    【讨论】:

      【解决方案3】:

      在不查看数据的情况下很难知道自己的对手是什么,但仅增加 14,000 个点 alpha 不太可能使“特殊点”` 足够突出。你可以试试这个:

      ## create artificial data set for this example
      set.seed(1)     # for reproducibility
      n  <- 1.4e4     # 14,000 points
      df <- data.frame(SeqIdentityMean  =rnorm(n, mean=rep(-3:3, each=n/7)), 
                       SeqIdentityStdDev=rnorm(n, mean=rep(-3:3, each=n/7)),
                       PfamA_ID=rep(1:7, each=n/7))
      df$PfamA_ID <- factor(df$PfamA_ID)
      
      ## you start here
      library(ggplot2)
      special.points <- sample(1:n, 7)
      ggp <- ggplot(df, aes(x=SeqIdentityMean, y=SeqIdentityStdDev, color=PfamA_ID))+
        geom_point(alpha=0.05)+
        geom_point(data=df[special.points,], aes(fill=PfamA_ID), color="black", alpha=1, size=4, shape=21)+
        scale_color_discrete(guide=guide_legend(override.aes=list(alpha=1, size=3)))+
        scale_fill_discrete(guide="none", drop=FALSE)
      ggp
      

      通过使用shape=21(实心圆圈),您可以给特殊点一个黑色轮廓,然后使用aes(fill=...)作为颜色。 IMO 这使他们更加突出。最直接的方法是使用仅包含特殊点的特定于层的数据集额外调用geom_point(...)

      最后,即使是这个人为的例子,这些组都被混合在一起了。如果您的真实数据是这种情况,我会倾向于尝试分面:

      ggp + facet_wrap(~PfamA_ID)
      

      这样做的好处是可以突出特殊点属于哪些组 (PfamA_ID),这在前面的情节中并不明显。

      关于您的代码的其他几点:

      1. 这是非常糟糕的做法,例如ggplot(df, aes(x=df$a, y=df$b, ...), ...)。改为使用:ggplot(df, aes(x=a, y=b, ...), ...)。映射的全部意义在于将美学(x、y、颜色等)与 df 中的列相关联,使用列名。您将列作为独立向量传递。
      2. 在示例中,我将df$PfamA_ID 设置为data.frame 中的一个因子,而不是在对aes(...) 的调用中。这很重要,因为事实证明特殊点子集缺少一些因子水平。如果换一种方式,特殊图层中的填充颜色将不会与主图层中的点颜色对齐。
      3. 当您设置alpha=0.05(或其他)时,图例将使用该 alpha,这使得图例几乎无用。要绕过这种用法:

        scale_color_discrete(guide=guide_legend(override.aes=list(alpha=1, size=3)))

      编辑:响应 OP 的最后评论/请求。

      所以听起来您想对除第一种颜色(不饱和红色)之外的所有内容使用 ggplot 的默认离散色标。这不是一个好主意,但这里有一种方法:

      # create custom color palette containing ggplot defaults for all but first color; use black for first color
      n.col <- length(levels(df$PfamA_ID))
      cols  <- c("#000000", hcl(h=seq(15, 375, length=n.col+1), l=65, c=100)[2:n.col])
      # set color and fill palette manually
      ggp <- ggplot(df, aes(x=SeqIdentityMean, y=SeqIdentityStdDev, color=PfamA_ID))+
        geom_point(alpha=0.05)+
        geom_point(data=df[special.points,], aes(fill=PfamA_ID), color="black", alpha=1, size=4, shape=21)+
        scale_color_manual(values=cols, guide=guide_legend(override.aes=list(alpha=1, size=3)))+
        scale_fill_manual(values=cols, guide="none", drop=FALSE)
      ggp
      

      【讨论】:

      • in=read.table("inout",sep="\t",header=TRUE) df=as.data.frame(in) df$PfamA_ID &lt;- factor(df$PfamA_ID) special.points &lt;- sample(1:5) # 1st column Pfam_ID has IDs, but only in 1st 5 rows that I want color coded on plot and legend library(ggplot2) ggplot(df, aes(x=Mean, y=StdDev))+ labs(title="T", x="X", y="Y")+ geom_point(alpha=0.03)+ geom_point(data=df[special.points,], aes(fill=PfamA_ID), color="black", alpha=1, size=2.5, shape=21)+ scale_color_discrete(guide=guide_legend(override.aes=list(alpha=1, size=3)))+ scale_fill_discrete(guide="none", drop=FALSE) 仍然没有图例 请帮忙!
      • 另外,PfamA_ID=rep(1:7, each=n/7) 有什么作用?
      • ggplot(df, aes(x=Mean, y=StdDev)) 替换为ggplot(df, aes(x=Mean, y=StdDev, color=Pfam_ID)),如答案所示。
      • Pfam_ID = rep(...) 只是在人工数据集中创建了一个分组列(2000 1,然后是 2000 2,等等)。它与您的真实数据无关。
      • 是的,成功了!非常感谢您耐心地帮助我:)
      猜你喜欢
      • 1970-01-01
      • 2014-02-14
      • 2011-11-30
      • 2011-01-22
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2017-06-29
      相关资源
      最近更新 更多