這些幾何圖形將參考線(有時稱為規則)添加到繪圖中,無論是水平的、垂直的還是對角線的(由斜率和截距指定)。這些對於注釋繪圖很有用。
用法
geom_abline(
  mapping = NULL,
  data = NULL,
  ...,
  slope,
  intercept,
  na.rm = FALSE,
  show.legend = NA
)
geom_hline(
  mapping = NULL,
  data = NULL,
  ...,
  yintercept,
  na.rm = FALSE,
  show.legend = NA
)
geom_vline(
  mapping = NULL,
  data = NULL,
  ...,
  xintercept,
  na.rm = FALSE,
  show.legend = NA
)
參數
- mapping
 - 
由
aes()創建的一組美學映射。 - data
 - 
該層要顯示的數據。有以下三種選擇:
如果默認為
NULL,則數據繼承自ggplot()調用中指定的繪圖數據。data.frame或其他對象將覆蓋繪圖數據。所有對象都將被強化以生成 DataFrame 。請參閱fortify()將為其創建變量。將使用單個參數(繪圖數據)調用
function。返回值必須是data.frame,並將用作圖層數據。可以從formula創建function(例如~ head(.x, 10))。 - ...
 - 
其他參數傳遞給
layer()。這些通常是美學,用於將美學設置為固定值,例如colour = "red"或size = 3。它們也可能是配對的 geom/stat 的參數。 - na.rm
 - 
如果
FALSE,則默認缺失值將被刪除並帶有警告。如果TRUE,缺失值將被靜默刪除。 - show.legend
 - 
合乎邏輯的。該層是否應該包含在圖例中?
NA(默認值)包括是否映射了任何美學。FALSE從不包含,而TRUE始終包含。它也可以是一個命名的邏輯向量,以精細地選擇要顯示的美學。 - xintercept, yintercept, slope, intercept
 - 
控製線位置的參數。如果設置了這些,則
data、mapping和show.legend將被覆蓋。 
細節
這些幾何體的行為與其他幾何體略有不同。您可以通過兩種方式提供參數:作為圖層函數的參數,或通過美學。如果您使用參數,例如geom_abline(intercept = 0, slope = 1) ,然後 geom 在幕後創建一個僅包含您提供的數據的新 DataFrame 。這意味著所有方麵的線條都是相同的;如果您希望它們在各個方麵有所不同,請自己構建 DataFrame 架並使用美學。
與大多數其他幾何圖形不同,這些幾何圖形不會從繪圖默認繼承美學,因為它們不理解繪圖中通常設置的 x 和 y 美學。它們也不影響 x 和 y 尺度。
美學
這些幾何體是使用 geom_line() 繪製的,因此它們支持相同的美學: alpha 、 colour 、 linetype 和 linewidth 。它們還各自具有控製線條位置的美學:
- 
geom_vline():xintercept - 
geom_hline():yintercept - 
geom_abline():slope和intercept 
也可以看看
有關向繪圖添加直線段的更通用方法,請參閱geom_segment()。
例子
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
# Fixed values
p + geom_vline(xintercept = 5)
p + geom_vline(xintercept = 1:5)
p + geom_hline(yintercept = 20)
p + geom_abline() # Can't see it - outside the range of the data
p + geom_abline(intercept = 20)
# Calculate slope and intercept of line of best fit
coef(lm(mpg ~ wt, data = mtcars))
#> (Intercept)          wt 
#>   37.285126   -5.344472 
p + geom_abline(intercept = 37, slope = -5)
# But this is easier to do with geom_smooth:
p + geom_smooth(method = "lm", se = FALSE)
#> `geom_smooth()` using formula = 'y ~ x'
# To show different lines in different facets, use aesthetics
p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  facet_wrap(~ cyl)
mean_wt <- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))
p + geom_hline(aes(yintercept = wt), mean_wt)
# You can also control other aesthetics
ggplot(mtcars, aes(mpg, wt, colour = wt)) +
  geom_point() +
  geom_hline(aes(yintercept = wt, colour = wt), mean_wt) +
  facet_wrap(~ cyl)
相關用法
- R ggplot2 geom_qq 分位數-分位數圖
 - R ggplot2 geom_spoke 由位置、方向和距離參數化的線段
 - R ggplot2 geom_quantile 分位數回歸
 - R ggplot2 geom_text 文本
 - R ggplot2 geom_ribbon 函數區和麵積圖
 - R ggplot2 geom_boxplot 盒須圖(Tukey 風格)
 - R ggplot2 geom_hex 二維箱計數的六邊形熱圖
 - R ggplot2 geom_bar 條形圖
 - R ggplot2 geom_bin_2d 二維 bin 計數熱圖
 - R ggplot2 geom_jitter 抖動點
 - R ggplot2 geom_point 積分
 - R ggplot2 geom_linerange 垂直間隔:線、橫線和誤差線
 - R ggplot2 geom_blank 什麽也不畫
 - R ggplot2 geom_path 連接觀察結果
 - R ggplot2 geom_violin 小提琴情節
 - R ggplot2 geom_dotplot 點圖
 - R ggplot2 geom_errorbarh 水平誤差線
 - R ggplot2 geom_function 將函數繪製為連續曲線
 - R ggplot2 geom_polygon 多邊形
 - R ggplot2 geom_histogram 直方圖和頻數多邊形
 - R ggplot2 geom_tile 矩形
 - R ggplot2 geom_segment 線段和曲線
 - R ggplot2 geom_density_2d 二維密度估計的等值線
 - R ggplot2 geom_map 參考Map中的多邊形
 - R ggplot2 geom_density 平滑密度估計
 
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Reference lines: horizontal, vertical, and diagonal。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
