ggplot2 中添加直线的geoms一共有几种。今天系统总结一下。
geom_hline 和geom_vline
library(ggplot2) library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.1
## ## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats': ## ## filter, lag
## The following objects are masked from 'package:base': ## ## intersect, setdiff, setequal, union
library(magrittr)
## Warning: package 'magrittr' was built under R version 3.6.1
plot <- mtcars %>% ggplot() + geom_point(aes(mpg, disp, colour = gear)) + theme_bw()
geom_hline 和geom_vline 分别用来添加水平线(horizontal)和垂直线(vertical)
plot + geom_hline(yintercept = 300) + geom_vline(xintercept = 20)

如果只是要添加单条或者多条确定的直线,则只需要设置yintercept和xintercept参数即可。当然,可以通过colour ,size ,linetype 来设置线条的外观等。 plot + geom_hline(yintercept = c(300, 400), colour = 'red', linetype = 2, size = 2) + geom_vline(xintercept = c(20, 25), colour = 'blue', linetype = 3, size = 3)

同样的,他们也支持aes 形式。 temp.data <- data.frame(x = c(1,2,3), y = c(100,200,300), stringsAsFactors = FALSE) plot + geom_hline(aes(yintercept = y, size = y), data = temp.data)

这个时候,一些配套的参数,比如show.legend 才开始起作用。 plot + geom_vline(aes(xintercept = y, size = y), data = temp.data, show.legend = FALSE)

geom_abline
geom_abline 可以用来画有斜率的直线,比如画对角线(截距为0,斜率为1)。
plot + geom_abline(slope = 1, intercept = 0) + scale_x_continuous(limits = c(0,300)) + scale_y_continuous(limits = c(0,300))
## Warning: Removed 11 rows containing missing values (geom_point).

同样的,也可以通过设置colour ,size ,linetype 来设置线条的外观等。 当然,geom_abline 也支持aes mapping方式来设置不同的直线。 temp.data <- data.frame(intercept = c(0, 0, 0), slope = c(1, 0.5, 0.8), stringsAsFactors = FALSE) temp.data$slope <- factor(temp.data$slope) library(ggnewscale)
## Warning: package 'ggnewscale' was built under R version 3.6.1
plot + ggnewscale::new_scale_color() + geom_abline(aes(intercept = intercept, slope = slope,colour = slope), data = temp.data, size = 1.5) + scale_x_continuous(limits = c(0,300)) + scale_y_continuous(limits = c(0,300))
## Scale for 'x' is already present. Adding another scale for 'x', which ## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which ## will replace the existing scale.
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning in Ops.factor(ranges$x[1], data$slope): '*' not meaningful for ## factors
## Warning in Ops.factor(ranges$x[2], data$slope): '*' not meaningful for ## factors
## Warning: Removed 3 rows containing missing values (geom_segment).

注意:这里用到了ggnewscale 包里面的new_scale_color() 函数。因为如果不使用它,会导致两个都mapping到colour的legend融合到一起,也就是后面的覆盖到了前面的。我们可以看看效果。
plot + geom_abline(aes(intercept = intercept, slope = slope,colour = slope), data = temp.data, size = 1.5) + scale_x_continuous(limits = c(0,300)) + scale_y_continuous(limits = c(0,300))
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning in Ops.factor(ranges$x[1], data$slope): '*' not meaningful for ## factors
## Warning in Ops.factor(ranges$x[2], data$slope): '*' not meaningful for ## factors
## Warning: Removed 3 rows containing missing values (geom_segment).

geom_segment
geom_segment 也可以用来添加直线,但是更为灵活,也就是可以设定起始点,而不是贯穿整个图。
通过设置起始点坐标添加直线其中,arrow 参数可以和arrow() 配合使用,来设置箭头。 plot + geom_segment(x = 15, xend = 30, y = 300, yend = 300, colour = 'red', linetype = 1, size = 2, lineend = 'round', arrow = arrow(ends = 'both')) + geom_segment(x = 15, xend = 30, y = 200, yend = 200, colour = 'red', linetype = 1, size = 2, lineend = 'butt') + geom_segment(x = 15, xend = 30, y = 100, yend = 100, colour = 'red', linetype = 1, size = 2, lineend = 'square')

aes
temp.data <- data.frame(x = c(15, 15, 15), xend = c(30, 30, 30), y = c(300, 200, 100), yend = c(300, 200, 100), stringsAsFactors = FALSE) plot + geom_segment(aes(x = x, xend = xend, y = y, yend = yend), data = temp.data)

------------------------------------------------------------------------
|