2016-06-10 119 views
1

我有一個問題與使用ggplot2的R中的直方圖有關。我一直在努力從兩個不同的變量中直接表示一些值。在嘗試並在Stackoverflow中尋找一些解決方案後,我知道了,但是......有人知道如何將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

謝謝!它幫助了我! –