基本问题,ggplot似乎没有做我期望的事情.
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
Run Code Online (Sandbox Code Playgroud)
我期待这个情节:
它正在密谋:
api*_*sch 10
情节ggplot2绘制不正确.它在表示传递给aes调用的数据所需的比例上绘制函数.它不关心你是否实际绘制数据geom.
为了说明这个问题,将实际数据点添加到绘图中并使x轴和y轴更加可见是有帮助的.下面的代码
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_point(shape = 1) +
geom_abline(intercept = 1, slope = -1, col = "red") +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0)
Run Code Online (Sandbox Code Playgroud)
由于您只想绘制上图的子部分,只需更正比例(并且不绘制轴和数据点).然后你得到你想要的结果:
ggplot(data=data.frame(x=c(-1,2), y=c(-1,2)), aes(x=x,y=y)) +
geom_blank() + # not necessary, taken from the OP's question
geom_abline(intercept = 1, slope = -1) +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
Run Code Online (Sandbox Code Playgroud)