我有一个数据集,该数据集适合使用geom_smooth
的趋势线,如下图第一幅图所示。很好,但是我想在其中添加geom_rug
。默认情况下,它被绘制在左侧和底部,如图2所示。尽管如此,我只想在底轴上使用它,所以请在下面的第三张图中使用sides = "b"
参数。
问题:假设geom_rug
也出现在垂直方向,则自动设置了第三幅图的y限制。我希望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(v0.2.1)于2019-03-08创建
答案 0 :(得分: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(v0.2.1)于2019-03-08创建