2017-09-27 7 views
2

ggplot2과 관련된 데이터 시각화 질문이 있습니다.밀도 플롯을 사용하여 특정 영역 음영 처리 - ggplot2

나는 어떻게 내 density_plot에서 특이성 영역을 그늘지게 할 수 있는지 알아 내려고하고있다. 나는 그것을 많이 봤는데 모든 솔루션을 시도.

내 코드는 다음과 같습니다 그래서

original_12 <- data.frame(sum=rnorm(100,30,5), sex=c("M","F")) 
cutoff_12 <- 35 
ggplot(data=original_12, aes(original_12$sum)) + geom_density() + 
    facet_wrap(~sex) + 
    geom_vline(data=original_12, aes(xintercept=cutoff_12), 
      linetype="dashed", color="red", size=1) 

이에서 :

enter image description here

ggplot2 shade area under density curve by group에 대한 질문은 나보다 다른 :

enter image description here

내가이 원하는 있다 그들은 다른 그룹과 그래프를 사용하기 때문입니다.

답변

1

패싯을 제외하고는 SO question과 유사하지만 추가 복잡성이 추가됩니다. PANEL 데이터의 이름을 "성별"로 바꾸고 기존의 미적 옵션에 맞게 올바르게 입력해야합니다. 원래의 "성"요소는 알파벳순으로 정렬되어 있습니다 (기본값은 data.frame 옵션). 처음에는 약간 혼란 스럽습니다.

당신이 ggplot 개체를 만들 수있는 플롯 "P"이름 확인 :

str(ggplot_build(p)$data[[1]]) 

'data.frame': 1024 obs. of 16 variables: 
$ y  : num 0.00114 0.00121 0.00129 0.00137 0.00145 ... 
$ x  : num 17 17 17.1 17.1 17.2 ... 
$ density : num 0.00114 0.00121 0.00129 0.00137 0.00145 ... 
$ scaled : num 0.0121 0.0128 0.0137 0.0145 0.0154 ... 
$ count : num 0.0568 0.0604 0.0644 0.0684 0.0727 ... 
$ n  : int 50 50 50 50 50 50 50 50 50 50 ... 
$ PANEL : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ... 
$ group : int -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... 
$ ymin : num 0 0 0 0 0 0 0 0 0 0 ... 
$ ymax : num 0.00114 0.00121 0.00129 0.00137 0.00145 ... 
$ fill : logi NA NA NA NA NA NA ... 
$ weight : num 1 1 1 1 1 1 1 1 1 1 ... 
$ colour : chr "black" "black" "black" "black" ... 
$ alpha : logi NA NA NA NA NA NA ... 
$ size : num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ... 
$ linetype: num 1 1 1 1 1 1 1 1 1 1 ... 
:

p <- ggplot(data=original_12, aes(original_12$sum)) + 
    geom_density() + 
    facet_wrap(~sex) + 
    geom_vline(data=original_12, aes(xintercept=cutoff_12), 
      linetype="dashed", color="red", size=1) 

ggplot 객체 데이터를 추출 할 수 ... 여기에 데이터의 구조는

PANEL 데이터의 이름을 바꾸고 원래 데이터 세트와 일치하도록 팩터를 사용해야하기 때문에 직접 사용할 수 없습니다.

to_fill <- data_frame(
    x = ggplot_build(p)$data[[1]]$x, 
    y = ggplot_build(p)$data[[1]]$y, 
    sex = factor(ggplot_build(p)$data[[1]]$PANEL, levels = c(1,2), labels = c("F","M"))) 

p + geom_area(data = to_fill[to_fill$x >= 35, ], 
       aes(x=x, y=y), fill = "red") 

enter image description here

1
#DATA 
set.seed(2) 
original_12 <- data.frame(sum=rnorm(100,30,5), sex=c("M","F")) 
cutoff_12 <- 35 

#Calculate density for each sex 
temp = do.call(rbind, lapply(split(original_12, original_12$sex), function(a){ 
    d = density(a$sum) 
    data.frame(sex = a$sex[1], x = d$x, y = d$y) 
})) 

#For each sex, seperate the data for the shaded area 
temp2 = do.call(rbind, lapply(split(temp, temp$sex), function(a){ 
    rbind(data.frame(sex = a$sex[1], x = cutoff_12, y = 0), a[a$x > cutoff_12,]) 
})) 

#Plot 
ggplot(temp) + 
    geom_line(aes(x = x, y = y)) + 
    geom_vline(xintercept = cutoff_12) + 
    geom_polygon(data = temp2, aes(x = x, y = y)) + 
    facet_wrap(~sex) + 
    theme_classic() 

enter image description here

: 당신은 여기에 ggplot 객체에서 데이터를 추출 할 수 있습니다