我有一个通用的plot_data(data)
方法。有时,输入的数据有我用填写变量所有NAS,这将导致错误ggplot2错误,当填充值都是NA:seq.default(h [1],h [2],length.out = n)中的错误:
Error in seq.default(h[1], h[2], length.out = n) :
'to' must be a finite number
例如:
df <- data.frame(
x = c(1, 2, 3, 4),
y = c(10, 15, 20, 25),
foo = factor(c(NA, NA, NA, "yes"), levels=c("yes", "no"))
)
ggplot(df, aes(x=x, y=y, fill=foo))+geom_bar(stat = "identity") # works
ggplot(df[1:3, ], aes(x=x, y=y, fill=foo))+geom_bar(stat = "identity") # error
我不明白为什么情节不应该渲染在情况2(只有全部灰色条)。有没有简单的方法来克服这一点?