2012-10-21 7 views
39

私は3列の行列を持っています。プロットは、列1および列2の値に基づいてポイントで行われますが、列2(6つの異なるグループ)に基づいて色付けされます。私は正常にすべての点をプロットすることができますが、最後のプロットグループ(グループ6)は、紫色に割り当てられ、他のグループのプロットをマスクします。プロットポイントをより透明にする方法はありますか?散布図のプロット点をRでより透明にする方法はありますか?

s <- read.table("/.../parse-output.txt", sep="\t") 
dim(s) 
[1] 67124  3 
x <- s[,1] 
y <- s[,2] 
z <- s[,3] 
cols <- cut(z, 6, labels = c("pink", "red", "yellow", "blue", "green", "purple")) 
plot(x, y, main= "Fragment recruitment plot - FR-HIT", ylab = "Percent identity", xlab = "Base pair position", col = as.character(cols), pch=16) 

答えて

56

あなたが(彼らはあなたの例のような要因であっても)色のあなたのベクトルを直接入力できる、そうでない場合、あなたはパッケージscalesに機能alphaを持っている:

library(scales) 
cols <- cut(z, 6, labels = c("pink", "red", "yellow", "blue", "green", "purple")) 
plot(x, y, main= "Fragment recruitment plot - FR-HIT", 
    ylab = "Percent identity", xlab = "Base pair position", 
    col = alpha(cols, 0.5), pch=16) 
# For an alpha of 0.5, i. e. a transparency of 50%. 
27

あなたはこのような何か?:

plot(1:10, col=rgb(1, 0, 0, 0.5), pch=16) 
points((1:10)+0.05, col=rgb(0, 0, 1, 0.5), pch=16) 

を意味するかの詳細について?rgbを参照してください。

+0

私が点のみの単一のグループにRGBの設定を追加することができそうです。これを複数のグループにどのように適用できますか? – Steve

+0

例:2つの異なる色:点((1:10)+0.05、col = rgb(c(0,0)、c(1,0)、c(0,1)、rep(0.5,2)) 、pch = 16) '(同じ6色のアプローチを使うことができます) – sgibb

10

ggplot2を使用する場合は、alpha引数を使用して、重複する点の透明度を設定できます。

library(ggplot2) 
ggplot(diamonds, aes(carat, price)) + geom_point(alpha = 1/40) 
15

透明性でコーディングすることができますカラー引数も同様です。 0(完全に透明)と255(完全に可視)の間の透明度をコードする2つの16進数だけです。私はこれまで色ベクトルに透明度を追加するためにこの関数を書いていましたが、おそらくここでは役に立ちますか?

addTrans <- function(color,trans) 
{ 
    # This function adds transparancy to a color. 
    # Define transparancy with an integer between 0 and 255 
    # 0 being fully transparant and 255 being fully visable 
    # Works with either color and trans a vector of equal length, 
    # or one of the two of length 1. 

    if (length(color)!=length(trans)&!any(c(length(color),length(trans))==1)) stop("Vector lengths not correct") 
    if (length(color)==1 & length(trans)>1) color <- rep(color,length(trans)) 
    if (length(trans)==1 & length(color)>1) trans <- rep(trans,length(color)) 

    num2hex <- function(x) 
    { 
    hex <- unlist(strsplit("ABCDEF",split="")) 
    return(paste(hex[(x-x%%16)/16+1],hex[x%%16+1],sep="")) 
    } 
    rgb <- rbind(col2rgb(color),trans) 
    res <- paste("#",apply(apply(rgb,2,num2hex),2,paste,collapse=""),sep="") 
    return(res) 
} 

いくつかの例:

cols <- sample(c("red","green","pink"),100,TRUE) 

# Fully visable: 
plot(rnorm(100),rnorm(100),col=cols,pch=16,cex=4) 

# Somewhat transparant: 
plot(rnorm(100),rnorm(100),col=addTrans(cols,200),pch=16,cex=4) 

# Very transparant: 
plot(rnorm(100),rnorm(100),col=addTrans(cols,100),pch=16,cex=4) 
+11

ベースRの'?adjustcolor'( 'grDevices'パッケージ)は完全にベクトル化されていないかもしれませんが、あなたのものとして。 –

+0

こんにちは、サチャ!答えを「ハイジャック」して申し訳ありません:-)。私は簡単に質問しています:カーブの中心からラベル(すなわちローディング)を離して配置するには、 'semPlot'や' qgraph' [優良パッケージ]という方法がありますか?曲線に沿ってではなく、「垂直」方向に離れています。私はむしろ低レベルのLaTeXルートに行くことに頼るつもりはない。ありがとうございました! –

+0

ありがとう!現在では、edge.label.position引数だけを使用して、ラベルの端にラベルが描画される場所を制御できます。私はそれを実装しようとします! –

4

あなたは16進コードを使用している場合は、アルファチャンネルを表現するためのコードの末尾2桁以上を追加することができます。例えば

赤い半透明性は:

plot(1:100, main="Example of Plot With Transparency") 
lines(1:100 + sin(1:100*2*pi/(20)), col='#FF000088', lwd=4) 
mtext("use `col='#FF000088'` for the lines() function") 

example plot of color with transparency

関連する問題