2015-09-06 269 views
2

我有~14,000个XY对来绘制,并且使用ggplot2这个。ggplot XY scatter - 如何更改选择点的alpha透明度?

由于分数很高,我不得不使用非常低的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')) 

回答

2

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

enter image description here

使用@ 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) 

enter image description here


编辑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) 

enter image description here

+0

如何为突出显示的点着色,以黑色(与显示的内容相反)为颜色,并添加文本 - 在另一列输入中,仅将特殊点添加到图例中?谢谢! – AksR

+0

@AksR请参阅编辑。 – zx8754

+0

哎呀,我之前的请求并不清楚。让我澄清一下。我怎样才能让每个不同的标注颜色,然后外面的图表有颜色和相应的文字(来自输入数据列#1,但只是special.points)的图例。所以我不希望它们全都是红色的,并且不希望它们全部是红色的,并且不希望它们全部是红色的,并且不希望它们全部是红色的,并且不希望它们全部是红色的,并且不希望它们全部是红色的,并且不希望它们全部是红色的,并对不起,是一个痛苦,但新的R,和全新ggplot :) – AksR

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

这里的诀窍是认识到阿尔法只是另一种美学。此外,我不会直接绘制14k点并依靠alpha,我只会使用2D分级。使用例如hexbin:

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

这是一个有点很难知道你在做什么反对,没有看到您的资料,但14000点单独增加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=...)的颜色。海事组织这使他们更加突出。最直接的方法是使用仅包含特殊点的图层特定数据集对geom_point(...)进行额外调用。

最后,即使有这个人为的例子,这些群体都被捣碎在一起。如果这是你的真实数据的情况下,我会倾向于尝试刻面:

ggp + facet_wrap(~PfamA_ID) 

这具有突出的优势,这组(PfamA_ID)的特殊点属于,这ISN”从早期的情节看来很明显。

一对夫妇对你的代码的其他要点:

  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 

+0

'在函数read.table =( “INOUT”,月= “\ t” 的,标题= TRUE) DF = as.data.frame(上) DF $ PfamA_ID < - 因子(DF $ PfamA_ID ) special.points < - sample(1:5)#第一列Pfam_ID具有ID,但仅在第一个5行中我想在图和图例上进行颜色编码 库(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)' 还没有图例帮助请帮助! – AksR

+0

另外,'PfamA_ID = rep(1:7,each = n/7)'做了什么? – AksR

+0

用'ggplot(df,aes(x = Mean,y = StdDev,color = Pfam_ID))'代替'ggplot(df,aes(x = Mean,y = StdDev))',就像在答案中一样。 – jlhoward