我有一個通用的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(只有全部灰色條)。有沒有簡單的方法來克服這一點?