| ggplot2中添加直線(xiàn)的geoms一共有幾種。今天系統(tǒng)總結(jié)一下。
 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分別用來(lái)添加水平線(xiàn)(horizontal)和垂直線(xiàn)(vertical)
 plot +geom_hline(yintercept = 300) +
 geom_vline(xintercept = 20)
 
 如果只是要添加單條或者多條確定的直線(xiàn),則只需要設(shè)置yintercept和xintercept參數(shù)即可。當(dāng)然,可以通過(guò)colour,size,linetype來(lái)設(shè)置線(xiàn)條的外觀等。 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)
 
 這個(gè)時(shí)候,一些配套的參數(shù),比如show.legend才開(kāi)始起作用。 plot +geom_vline(aes(xintercept = y, size = y), data = temp.data,
 show.legend = FALSE)
 
 geom_abline
geom_abline可以用來(lái)畫(huà)有斜率的直線(xiàn),比如畫(huà)對(duì)角線(xiàn)(截距為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).
 
 同樣的,也可以通過(guò)設(shè)置colour,size,linetype來(lái)設(shè)置線(xiàn)條的外觀等。 當(dāng)然,geom_abline也支持aesmapping方式來(lái)設(shè)置不同的直線(xiàn)。 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()函數(shù)。因?yàn)槿绻皇褂盟?,?huì)導(dǎo)致兩個(gè)都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也可以用來(lái)添加直線(xiàn),但是更為靈活,也就是可以設(shè)定起始點(diǎn),而不是貫穿整個(gè)圖。
 通過(guò)設(shè)置起始點(diǎn)坐標(biāo)添加直線(xiàn)其中,arrow參數(shù)可以和arrow()配合使用,來(lái)設(shè)置箭頭。 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)
 
 ------------------------------------------------------------------------
 |