当我使用绘图时,geom_area()我希望它表现得很好geom_bar(),但我对这种缺失值的行为感到有些困惑.
require(dplyr)
require(ggplot2)
set.seed(1)
test <- data.frame(x=rep(1:10,3), y=abs(rnorm(30)), z=rep(LETTERS[1:3],10)) %>% arrange(x,z)
# I also have no idea why geom_area needs the data.frame to be sorted first.
test[test$x==4,"y"] <- NA
ggplot(test, aes(x, y, fill=z)) + geom_bar(stat="identity", position="stack")
Run Code Online (Sandbox Code Playgroud)
生成此堆积条形图.

但是,如果我更改为stack_area(),它会插入缺失值.
> ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack")
Warning message:
Removed 3 rows containing missing values (position_stack).
Run Code Online (Sandbox Code Playgroud)

如果我加入na.rm=FALSE或na.rm=TRUE没有任何区别.
ggplot(test,aes(x,y,fill = z))+ geom_area(stat ="identity",position ="stack",na.rm = TRUE)警告消息:删除了包含缺失值的3行(position_stack)

ggplot(test,aes(x,y,fill = z))+ geom_area(stat ="identity",position ="stack",na.rm = FALSE)警告消息:删除了包含缺失值的3行(position_stack). …
我有一个数据集,其中 X 值是从 1 到千分之几的整数,并且想要绘制平均值 Y 以及围绕该平均值的离散度度量。我遇到的问题是缺少一些 X 值。因此,当使用 geom_line 和 geom_ribbon 函数时,绘图是连续的,我找不到办法让它在没有数据的地方留下空白。
这是一个可重现的模型示例。
data.1 <-read.csv(text = "
Treatment, X, Y_value
A,1,120.5
B,1,123.6
C,1,100.4
A,2,120.9
B,2,123.9
C,2,101.0
A,3,122.3
B,3,126.6
C,3,102.3
A,6,124.8
B,6,128.0
C,6,105.5
A,7,129.5
B,7,129.4
C,7,108.9
A,8,132.9
B,8,130.6
C,8,113.9
A,9,137.6
B,9,136.0
C,9,115.3
A,10,138.4
B,10,139.6
C,10,118.9
A,11,143.9
B,11,145.9
C,11,126.6
")
data.1 <- data.1 %>% group_by(X) %>% summarise(mean.y = mean(Y_value),
sd.y = sd(Y_value))
library(ggplot2)
ggplot(data.1, aes(X, mean.y)) +
geom_line(color="red") +
geom_ribbon(aes(ymin=mean.y-sd.y, ymax=mean.y+sd.y), alpha=0.4) +
scale_x_continuous(limits=c(0,11), breaks = c(seq(min(0),max(11), length.out = 12)))+
theme_bw() …Run Code Online (Sandbox Code Playgroud)