I have the following function:
我有以下功能:
miss.case = function(x){
y = apply(x, 1, is.na)
y = apply(y, 2, sum)
return(y)
}
miss.hist = function(df, percent=T) {
m = miss.case(df)
d = data.frame(number.of.NA = m)
max.miss = max(m)
min.miss = min(m)
if (percent) {
d$percent = (d$number.of.NA/sum(d$number.of.NA))*100
g = ggplot(data = d, aes(x = number.of.NA)) +
geom_bar(aes(y = ((..count..)/sum(..count..))*100)) +
scale_y_continuous('percent') +
xlab("Number of NAs") +
scale_x_discrete(breaks=min.miss:max.miss)
return(g)
}
else {
g = ggplot(data = d, aes(x = number.of.NA)) +
geom_histogram() +
xlab("Number of NAs") +
scale_x_discrete(breaks=min.miss:max.miss)
return(g)
}
}
Which makes a nice histogram of missing data by case with ggplot2. Almost. To see, try with some test data:
通过ggplot2的情况下,这是一个很好的缺失数据直方图。几乎。要查看,请尝试使用一些测试数据:
#make some test data
test.data = as.data.frame(iris)
set.seed(1)
which.remove = cbind(sample(1:150, 250, T),
sample(1:5, 250, T))
for (row in 1:nrow(which.remove)) {
test.data[which.remove[row,1],which.remove[row,2]] = NA
}
#plot missing
miss.hist(test.data)
Which should give you this:
哪个应该给你这个:
You see what is wrong. The right part of the plot is weirdly empty. Now you may think, this is easy to solve with setting the limits, i.e.: limits=c(min.miss, max.miss)
. But no, this fixes the problem, but removes the ticks!
你看错了什么。情节的右边部分很奇怪。现在您可能会认为,通过设置限制很容易解决,即:limits = c(min.miss,max.miss)。但不,这解决了问题,但删除了滴答声!
Changing the order of them does not make a difference. How do I fix both problems?
改变它们的顺序并没有什么不同。我该如何解决这两个问题?
1 个解决方案
#1
You are using a discrete scale with an integer
vector. Transform it to a factor
instead
您正在使用具有整数向量的离散比例。将其转换为因子
g = ggplot(data = d, aes(x = factor(number.of.NA,levels=as.character(seq(0,max.miss,1))))) +
#1
You are using a discrete scale with an integer
vector. Transform it to a factor
instead
您正在使用具有整数向量的离散比例。将其转换为因子
g = ggplot(data = d, aes(x = factor(number.of.NA,levels=as.character(seq(0,max.miss,1))))) +