ggplot2:将对角线添加到网格

时间:2016-08-12 21:13:06

标签: r ggplot2

是否可以通过向网格添加对角线的方式调整ggplot2

显然,默认网格由垂直和水平线组成:

df <- data.frame(a = sample(0:100, 100, T), b = sample(0:100, 100, T))
gg <- ggplot() + geom_point(data = df, aes(x = a, y = b))
gg

plot1

我正在考虑的是修改默认行为以实现由垂直,水平和对角线组成的网格。实现这一目标的一种相当混乱的方法是使用段:

d <- data.frame(x = c(0,0,0,0,25,50,75),
                y = c(75,50,25,0,0,0,0),
                xend = c(25,50,75,100,100,100,100),
                yend = c(100,100,100,100,75,50,25))


gg + 
  geom_segment(data = d, aes(x = x, y = y, xend = xend, yend = yend), colour = "white")

plot2

这似乎是一种解决方法。但所需的实际段数取决于数据的范围。考虑到对角线之间所需的空间,可以编写一个函数来计算段数,ggplot2仍会将此“变通方数据”视为实际数据。这是我想要避免的。

1 个答案:

答案 0 :(得分:4)

CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day) 怎么样?

geom_abline

enter image description here