使用 xlim ylim

> library(ggplot2)
> ggplot(iris,aes(Sepal.Width)) + geom_density() + xlim(1,3.5)

使用 xlim 或 ylim 不會切割圖,ggplot 在呼叫 stat 函式之前對資料進行子集(在本例中為 stat_density)。你可以在警告訊息中看到它。

Warning message:
Removed 19 rows containing non-finite values (stat_density).