【问题标题】:Plot NA counts in a histogram在直方图中绘制 NA 计数
【发布时间】:2016-06-10 17:03:23
【问题描述】:

我有一个关于 R 中使用 ggplot2 的直方图的问题。我一直在努力从两个不同的变量中表示直方图中的一些值。在尝试并在 Stackoverflow 中寻找一些解决方案后,我得到了它,但是......有人知道如何打印 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")

这是输出:

关于两个变量值但没有缺失条的图像:

【问题讨论】:

    标签: r ggplot2


    【解决方案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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多