ggplot XY散射 - 如何更改选择点的Alpha透明度?

时间:2015-09-06 11:43:19

标签: r ggplot2 alpha scatter

我有~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'))

3 个答案:

答案 0 :(得分:5)

只需在数据集中创建一个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))

enter image description here

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

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

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

答案 1 :(得分:3)

我们可以使用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])

## 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)

enter image description here

使用@ jlhoward的示例数据:

annotate("text",...)

编辑1: 我们可以添加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)

#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)

enter image description here

编辑2:

    (bank is a static int variable declared up top)
    boolean valid = true;
    Scanner scan = new Scanner(System.in);


    while (valid) {
        try {
        System.out.println("How much money do you want to convert to chips?");
        bank = scan.nextInt();
        valid = false;
        } catch(InputMismatchException ex) {
            System.out.println("Thats not right try entering a number between 0 and 2,147,483,647");
            //bank = scan.nextInt();
            continue;
        } 

    }

enter image description here

答案 2 :(得分:2)

在没有看到你的数据的情况下知道你面对的是有点困难,但仅增加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)))

  4. 编辑:对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