2016-06-10 7 views
1

ggplot2를 사용하여 R의 히스토그램과 관련된 질문이 있습니다. 나는 두 개의 다른 변수로부터 히스토그램의 일부 값을 표현하려고 노력하고있다. 노력하고 Stackoverflow에서 몇 가지 해결책을 찾은 후에 나는 그것을 얻었지만 ... 누군가가 두 개의 변수에 missings를 비교하기 위해 새로운 칼럼으로 NAs 카운트를 인쇄하는 방법을 알고 있습니까? 여기 히스토그램에서 플롯 NA 카운트

는 R 코드 :

i<-"ADL_1_bathing" 
j<-"ADL_1_T2_bathing" 

t1<-data.frame(datosMedicos[,i]) 
colnames(t1)<-"datos" 
t2<-data.frame(datosMedicos[,j]) 
colnames(t2)<-"datos" 
t1$time<-"t1" 
t2$time<-"t2" 

juntarParaGrafico<-rbind(t1,t2) 

ggplot(juntarParaGrafico, aes(datos, fill = time)) + 
    geom_histogram(col="darkblue",alpha = 0.5, aes(y = ..count..), binwidth = 0.2, position = 'dodge', na.rm = F) + 
    theme(legend.justification = c(1, 1), legend.position=c(1, 1))+ 
    labs(title=paste0("Distribution of ",i), x=i, y="Count") 

그리고 이것은 출력 : 두 변수의 값에 대한하지만 누락 된 바없는

이미지 :

enter image description here

답변

1

당신이 할 수 NAs b4 작의 수를 요약 해보십시오. 이건 어때?

library(ggplot2) 
library(dplyr) 

df1 = data.frame(a = rnorm(1:20)) 
df1[sample(1:20, 5),] = NA 
df2 = data.frame(a = rnorm(1:20)) 
df2[sample(1:20, 3),] = NA 
df2$time = "t2" 
df1$time = "t1" 
df = rbind(df1, df2) 
df %>% group_by(time) %>% summarise(numNAs = sum(is.na(a))) 
histogramDF= df %>% group_by(time) %>% summarise(numNAs = sum(is.na(a))) 

qplot(x=time, y = numNAs, fill=time, data = histogramDF, stat='identity',  geom="histogram") 
+0

감사합니다. 그것은 나를 도왔다! –