箱線圖簡潔地顯示了連續變量的分布。它可視化五個匯總統計數據(中位數、兩個鉸鏈和兩個胡須),以及所有"outlying" 點。
用法
geom_boxplot(
  mapping = NULL,
  data = NULL,
  stat = "boxplot",
  position = "dodge2",
  ...,
  outlier.colour = NULL,
  outlier.color = NULL,
  outlier.fill = NULL,
  outlier.shape = 19,
  outlier.size = 1.5,
  outlier.stroke = 0.5,
  outlier.alpha = NULL,
  notch = FALSE,
  notchwidth = 0.5,
  varwidth = FALSE,
  na.rm = FALSE,
  orientation = NA,
  show.legend = NA,
  inherit.aes = TRUE
)
stat_boxplot(
  mapping = NULL,
  data = NULL,
  geom = "boxplot",
  position = "dodge2",
  ...,
  coef = 1.5,
  na.rm = FALSE,
  orientation = NA,
  show.legend = NA,
  inherit.aes = TRUE
)
參數
- mapping
 - 
由
aes()創建的一組美學映射。如果指定且inherit.aes = TRUE(默認),它將與繪圖頂層的默認映射組合。如果沒有繪圖映射,則必須提供mapping。 - data
 - 
該層要顯示的數據。有以下三種選擇:
如果默認為
NULL,則數據繼承自ggplot()調用中指定的繪圖數據。data.frame或其他對象將覆蓋繪圖數據。所有對象都將被強化以生成 DataFrame 。請參閱fortify()將為其創建變量。將使用單個參數(繪圖數據)調用
function。返回值必須是data.frame,並將用作圖層數據。可以從formula創建function(例如~ head(.x, 10))。 - position
 - 
位置調整,可以是命名調整的字符串(例如
"jitter"使用position_jitter),也可以是調用位置調整函數的結果。如果需要更改調整設置,請使用後者。 - ...
 - 
其他參數傳遞給
layer()。這些通常是美學,用於將美學設置為固定值,例如colour = "red"或size = 3。它們也可能是配對的 geom/stat 的參數。 - outlier.colour, outlier.color, outlier.fill, outlier.shape, outlier.size, outlier.stroke, outlier.alpha
 - 
異常值的默認美學。設置為
NULL以繼承用於盒子的美學。萬一您同時指定了美國和英國的顏色拚寫,則美國拚寫優先。
有時隱藏異常值可能很有用,例如將原始數據點疊加在箱線圖上時。隱藏異常值可以通過設置
outlier.shape = NA來實現。重要的是,這不會刪除離群值,而隻是隱藏它們,因此為 y 軸計算的範圍將與顯示的離群值和隱藏的離群值相同。 - notch
 - 
如果
FALSE(默認)製作標準箱線圖。如果是TRUE,則繪製缺口箱線圖。缺口用於比較組;如果兩個盒子的缺口不重疊,則表明中位數顯著不同。 - notchwidth
 - 
對於缺口箱線圖,缺口相對於主體的寬度(默認為
notchwidth = 0.5)。 - varwidth
 - 
如果
FALSE(默認)製作標準箱線圖。如果是TRUE,則繪製框的寬度與組中觀測值數量的平方根成正比(可能使用weight美學進行加權)。 - na.rm
 - 
如果
FALSE,則默認缺失值將被刪除並帶有警告。如果TRUE,缺失值將被靜默刪除。 - orientation
 - 
層的方向。默認值 (
NA) 自動根據美學映射確定方向。萬一失敗,可以通過將orientation設置為"x"或"y"來顯式給出。有關更多詳細信息,請參閱方向部分。 - show.legend
 - 
合乎邏輯的。該層是否應該包含在圖例中?
NA(默認值)包括是否映射了任何美學。FALSE從不包含,而TRUE始終包含。它也可以是一個命名的邏輯向量,以精細地選擇要顯示的美學。 - inherit.aes
 - 
如果
FALSE,則覆蓋默認美學,而不是與它們組合。這對於定義數據和美觀的輔助函數最有用,並且不應繼承默認繪圖規範的行為,例如borders()。 - geom, stat
 - 
用於覆蓋
geom_boxplot()和stat_boxplot()之間的默認連接。 - coef
 - 
晶須的長度為 IQR 的倍數。默認為 1.5。
 
方向
該幾何體以不同的方式對待每個軸,因此可以有兩個方向。通常,方向很容易從給定映射和使用的位置比例類型的組合中推斷出來。因此,ggplot2 默認情況下會嘗試猜測圖層應具有哪個方向。在極少數情況下,方向不明確,猜測可能會失敗。在這種情況下,可以直接使用 orientation 參數指定方向,該參數可以是 "x" 或 "y" 。該值給出了幾何圖形應沿著的軸,"x" 是您期望的幾何圖形的默認方向。
統計匯總
下鉸鏈和上鉸鏈對應於第一和第三四分位數(第 25 個和第 75 個百分位數)。這與 boxplot() 函數使用的方法略有不同,並且對於小樣本可能會很明顯。有關如何計算 boxplot() 鉸鏈位置的更多信息,請參閱boxplot.stats()。
上須線從鉸鏈延伸到最大值,距離鉸鏈不超過 1.5 * IQR(其中 IQR 是 inter-quartile 範圍,或第一和第三四分位數之間的距離)。下部須線從鉸鏈延伸至鉸鏈的至多 1.5 * IQR 的最小值。超出晶須末端的數據稱為 "outlying" 點,並單獨繪製。
在缺口箱形圖中,缺口延伸 1.58 * IQR / sqrt(n) 。這為比較中位數提供了大約 95% 的置信區間。請參閱McGill 等人。 (1978)了解更多細節。
美學
geom_boxplot() 理解以下美學(所需的美學以粗體顯示):
- 
x或者y - 
lower或者xlower - 
upper或者xupper - 
middle或者xmiddle - 
ymin或者xmin - 
ymax或者xmax - 
alpha - 
colour - 
fill - 
group - 
linetype - 
linewidth - 
shape - 
size - 
weight 
在 vignette("ggplot2-specs") 中了解有關設置這些美學的更多信息。
計算變量
這些是由層的 'stat' 部分計算的,可以使用 delayed evaluation 訪問。 stat_boxplot() 提供以下變量,其中一些取決於方向:
- 
after_stat(width)
箱線圖的寬度。 - 
after_stat(ymin)或者after_stat(xmin)
下須須 = 大於或等於下須須的最小觀測值 - 1.5 * IQR。 - 
after_stat(lower)或者after_stat(xlower)
下鉸鏈,25% 分位數。 - 
after_stat(notchlower)
凹口下緣 = 中位數 - 1.58 * IQR /sqrt(n)。 - 
after_stat(middle)或者after_stat(xmiddle)
中位數,50% 分位數。 - 
after_stat(notchupper)
缺口上邊 = 中位數 + 1.58 * IQR /sqrt(n)。 - 
after_stat(upper)或者after_stat(xupper)
上鉸鏈,75% 分位數。 - 
after_stat(ymax)或者after_stat(xmax)
上胡須 = 小於或等於上胡須的最大觀測值 + 1.5 * IQR。 
也可以看看
geom_quantile() 用於連續 x ,geom_violin() 用於更豐富的分布顯示,geom_jitter() 用於小數據的有用技術。
例子
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot()
# Orientation follows the discrete axis
ggplot(mpg, aes(hwy, class)) + geom_boxplot()
p + geom_boxplot(notch = TRUE)
#> Notch went outside hinges
#> ℹ Do you want `notch = FALSE`?
#> Notch went outside hinges
#> ℹ Do you want `notch = FALSE`?
p + geom_boxplot(varwidth = TRUE)
p + geom_boxplot(fill = "white", colour = "#3366FF")
# By default, outlier points match the colour of the box. Use
# outlier.colour to override
p + geom_boxplot(outlier.colour = "red", outlier.shape = 1)
# Remove outliers when overlaying boxplot with original data points
p + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)
# Boxplots are automatically dodged when any aesthetic is a factor
p + geom_boxplot(aes(colour = drv))
# You can also use boxplots with continuous x, as long as you supply
# a grouping variable. cut_width is particularly useful
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot()
#> Warning: Continuous x aesthetic
#> ℹ did you forget `aes(group = ...)`?
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25)))
# Adjust the transparency of outliers using outlier.alpha
ggplot(diamonds, aes(carat, price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.25)), outlier.alpha = 0.1)
# \donttest{
# It's possible to draw a boxplot with your own computations if you
# use stat = "identity":
set.seed(1)
y <- rnorm(100)
df <- data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = max(y)
)
ggplot(df, aes(x)) +
  geom_boxplot(
   aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
   stat = "identity"
 )
# }
相關用法
- R ggplot2 geom_bar 條形圖
 - R ggplot2 geom_bin_2d 二維 bin 計數熱圖
 - R ggplot2 geom_blank 什麽也不畫
 - R ggplot2 geom_qq 分位數-分位數圖
 - R ggplot2 geom_spoke 由位置、方向和距離參數化的線段
 - R ggplot2 geom_quantile 分位數回歸
 - R ggplot2 geom_text 文本
 - R ggplot2 geom_ribbon 函數區和麵積圖
 - R ggplot2 geom_hex 二維箱計數的六邊形熱圖
 - R ggplot2 geom_jitter 抖動點
 - R ggplot2 geom_point 積分
 - R ggplot2 geom_linerange 垂直間隔:線、橫線和誤差線
 - 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 平滑密度估計
 - R ggplot2 geom_abline 參考線:水平、垂直和對角線
 
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 A box and whiskers plot (in the style of Tukey)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
