2016-12-31 9 views
2

g에서 ggplot2의 알파 값은 종종 R에서의 오버 플로팅을 돕는 데 사용됩니다. 어두운 색은 많은 관측치가 떨어지는 영역을 나타내며 밝은 색은 소수의 관측치 만 제외하는 영역을 나타냅니다. 이것을 뒤집을 수 있습니까? 그래서, 일반적으로 관측치가 적은 outlier는 어두운 곳으로 강조되지만 대다수의 데이터 (일반적으로 많은 관측치가있는)는 가볍게 강조됩니다.ggplot2에서 알파 값을 역방향으로 오버 플로팅

myDat <- data.frame(x=rnorm(10000,0,1),y=rnorm(10000,0,1)) 
qplot(x=x, y=y, data=myDat, alpha=0.2) 

더 희귀 한 관찰 멀리 센터 (0,0)에서 가벼운 : 아래

은 MWE이다. 어떻게 그걸 뒤집을 수 있니? 어떤 아이디어에 감사드립니다.

+1

정확한 요구 사항에 따라 [geom_density_2d] (http://docs.ggplot2.org/current/geom_density_2d.html)를 고려해 볼 수 있습니다. 예 : 두 번째 마지막 예는 [scale_fill_gradient] (http://docs.ggplot2.org/current/scale_gradient.html)와 함께 사용자가 선택한 '낮음'및 '높음'색상을 설정할 수 있습니다. – Henrik

답변

5

불투명도가 중심에서 더 멀어지면서 각 점의 알파 값을 별도로 설정할 수 있습니다. 특이 점수 (점수가 높은 사람이 어두운 색을 할당 할 수있는 대신에 알파 값을 사용하는)로이

p = 2 # adjust this parameter to set how steeply opacity ncreases with distance 
d = (myDat$x^2 + myDat$y^2)^p 
al = d/max(d) 
ggplot(myDat, aes(x=x, y=y)) + geom_point(alpha = al) 

enter image description here

3

비슷해 중심에서 마할 라 노비스 거리 이것을 시도 :

myDat <- data.frame(x=rnorm(10000,0,1),y=rnorm(10000,0,1)) 
mu <- colMeans(myDat) 

# assuming x, y independent, if not we can always calculate a non-zero cov(x,y) 
sigma <- matrix(c(var(myDat$x), 0, 0, var(myDat$y)), nrow=2) 
# use (squared) *Mahalanobis distance* as outlier score 
myDat$outlier.score <- apply(myDat, 1, function(x) t(x-mu)%*%solve(sigma)%*%(x-mu)) 
qplot(x=x, y=y, data=myDat, col=outlier.score) + 
    scale_color_gradient(low='white', high='blue') 

enter image description here

# assuming x, y are not independent 
sigma <- matrix(c(var(myDat$x), cov(myDat$x, myDat$y), cov(myDat$x, myDat$y), var(myDat$y)), nrow=2) 
# use (squared) *Mahalanobis distance* from centroid as outlier score 
myDat$outlier.score <- apply(myDat, 1, function(x) t(x-mu)%*%solve(sigma)%*%(x-mu)) 
qplot(x=x, y=y, data=myDat, col=outlier.score) + 
    scale_color_gradient(low='white', high='blue') 

enter image description here

관련 문제