2

我有一个关于 ggplot 的小问题,我想知道如何让数据点从 0 线开始,而不会在左侧、右侧和底部留下那个小间隙。

这是我的代码:

hov.dat <- structure(list(x = c(3L, 3L, 9L, 25L, 25L, 27L, 30L, 39L, 49L, 
56L, 60L, 65L), y = c(55, 54, 34.33, 34, 75.66, 44, 56.55, 54, 
27.34, 30.75, 19.04, 25.29)), .Names = c("x", "y"), class = "data.frame", row.names = c(NA, 
-12L))

with(hov.dat, plot(x, y))

qplot(x, y, data=hov.dat, geom=c('point', 'smooth'), method='lm', formula=y ~ ns(x, 3))

在此处输入图像描述

谁能帮我解决我应该编写什么代码来删除图中的左侧、右侧和底部间隙(图中标有箭头)

4

1 回答 1

1

您必须将expand参数提供给scale_x_continuous(对于 y 轴):

qplot(data=d,x,y,geom=c("point","smooth"),method="lm") + 
  scale_y_continuous(expand=c(0,0)) + 
  scale_x_continuous(expand=c(0,0))

我无法执行 function ns,所以我没有使用你的公式。

在此处输入图像描述

expand:长度为 2 的数值向量,给出乘法和加法扩展常数。这些常数确保数据放置在远离轴的位置。

请注意,您还可以扩展平滑函数的插值,如下图所示,恕我直言,它看起来更好。在 CV 上看到这个问题

gplot(hov.dat,aes(x=x,y=y)) + geom_point() + stat_smooth(method="lm",fullrange=T) + scale_x_continuous(limits=c(-5,70),expand=c(0,0))

在此处输入图像描述

于 2013-09-06T10:04:18.617 回答