2015-09-06 271 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