我有一个数据集,我适合使用geom_smooth
的趋势线,如下图所示。这很好,但我想加入geom_rug
。默认情况下,它在左侧和底部都绘制,如第二个图所示。不过,我只想在底部轴上,所以请使用下面第三张图中的sides = "b"
参数。
问题:第三个图中的y限制是自动设置的,假设geom_rug
也出现在垂直方面。我希望y限制与第一个图中的相同。 (我认为它们应该与第一个图中的相同。为什么所有额外空间都没有被绘制在其中?!)当然,我可以手动设置y限制,但有一种直接的方式阻止geom_rug
不必要地重置y限制?
# Set RNG seed
set.seed(42)
# Create dummy data set
df <- data.frame(x = runif(1000, 0, 10))
df$y <- df$x + rnorm(nrow(df), 0, 5)
# Load library
library(ggplot2)
# Plot trend line
ggplot(df, aes(x, y)) + geom_smooth()
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
# Plot trends line with rug on left and bottom
ggplot(df, aes(x, y)) + geom_smooth() + geom_rug()
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
# Plot trends line with rug on bottom
ggplot(df, aes(x, y)) + geom_smooth() + geom_rug(sides = "b")
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
由reprex package创建于2019-03-08(v0.2.1)
我一问,我就知道了。在这里,我只是将y
设置为NULL
并且没有调整大小。
# Set RNG seed
set.seed(42)
# Create dummy data set
df <- data.frame(x = runif(1000, 0, 10))
df$y <- df$x + rnorm(nrow(df), 0, 5)
# Load library
library(ggplot2)
# Plot trends line with rug on left and bottom
ggplot(df, aes(x, y)) + geom_smooth() + geom_rug(sides = "b", aes(y = NULL))
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
由reprex package创建于2019-03-08(v0.2.1)