不要丢失ggplot2中的N / A值并删除异常值

问题描述 投票:-3回答:1

我想用N / A值绘制我的数据的直方图/条形图。在我尝试使用ggplot2时,所有非有限值都会自动删除。是否可以计算我们拥有多少人并投入情节?我想解决这个问题或类(整数,数字,字符,日期等)。

ggplot(tmp, aes(x = x, y=(..count..)/sum(..count..))) + 
  geom_bar(fill="#003399") + 
  labs(title = "...", x = "Variable Values", y = "Frequency")

我也有问题。如何从ggplot面板中自动删除5%的最低值和5%的最高值(异常值)?直方图的组成将更加透明。

r ggplot2 geom-bar
1个回答
1
投票

也许这就是你要找的东西:

# Generate a 'toy dataset' with some missing values in y
set.seed(1234)
n <- 100
tmp <- data.frame(x = sample(LETTERS[1:5], n, replace=T),
                  y = rnorm(n))
tmp$y[sample(1:n,10)] <- NA
summary(tmp)


tmp$miss <- "No missing"
tmp$miss[is.na(tmp$y)] <- "Missing"
ggplot(tmp, aes(x = x, y=(..count..)/sum(..count..))) + 
  geom_bar(aes(group=miss, fill=miss), position="stack") + 
  labs(title = "...", x = "Variable Values", y = "Frequency")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.