[英]Why is fullrange=TRUE not working for geom_smooth in ggplot2?
[英]ggplot2: geom_smooth confidence band does not extend to edge of graph, even with fullrange=TRUE
你可能需要添加coord_cartesian
除了scale_x/y_continuous
。 scale_x/y_continuous
會刪除圖形范圍之外的點,但是coord_cartesian
會覆蓋它並使用所有數據,即使其中一些數據在圖形中不可見。 在圖中,紅色點的置信帶在帶的頂部超過圖形的y范圍時結束。
在圖形的擴展范圍內沒有實際的“數據”,但是geom_smooth
生成置信帶而生成的點視為“數據”,以決定要繪制的內容。
看下面的例子。 第一個圖僅使用scale_x/y_continuous
。 第二個添加了coord_cartesian
,但是請注意,仍未繪制置信帶。 在第三coord_cartesian
圖中,我們仍然使用coord_cartesian
,但是我們向下擴展了scale_y_continuous
范圍,以便將置信帶中低於零的點包括在y范圍中。 但是, coord_cartesian
決定了實際繪制的范圍,也防止了超出范圍的點被排除。
我實際上發現這種行為令人困惑。 我本以為您可以只將coord_cartesian
與所需的x和y范圍一起使用,並且仍然將置信帶和回歸線繪制到圖形的邊緣。 無論如何,希望這能為您提供所需的東西。
p1 = ggplot(mtcars, aes(wt, mpg, colour=factor(am))) +
geom_smooth(fullrange=TRUE, method="lm") +
scale_x_continuous(expand=c(0,0), limits=c(0,10)) +
scale_y_continuous(expand=c(0,0), limits=c(0,100)) +
ggtitle("scale_x/y_continuous")
p2 = ggplot(mtcars, aes(wt, mpg, colour=factor(am))) +
geom_smooth(fullrange=TRUE, method="lm") +
scale_x_continuous(expand=c(0,0), limits=c(0,10)) +
scale_y_continuous(expand=c(0,0), limits=c(0,100)) +
coord_cartesian(xlim=c(0,10), ylim=c(0,100)) +
ggtitle("Add coord_cartesian; same y-range")
p3 = ggplot(mtcars, aes(wt, mpg, colour=factor(am))) +
geom_smooth(fullrange=TRUE, method="lm") +
scale_x_continuous(expand=c(0,0), limits=c(0,10)) +
scale_y_continuous(expand=c(0,0), limits=c(-50,100)) +
coord_cartesian(xlim=c(0,10), ylim=c(0,100)) +
ggtitle("Add coord_cartesian; expanded y-range")
gridExtra::grid.arrange(p1, p2, p3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.