stat_summary_2d() 是 stat_summary() 的二維變體。 stat_summary_hex() 是 stat_summary_2d() 的六邊形變體。數據被分為 x 和 y 定義的 bin,然後每個單元中 z 的值用 fun 進行匯總。
用法
stat_summary_2d(
  mapping = NULL,
  data = NULL,
  geom = "tile",
  position = "identity",
  ...,
  bins = 30,
  binwidth = NULL,
  drop = TRUE,
  fun = "mean",
  fun.args = list(),
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)
stat_summary_hex(
  mapping = NULL,
  data = NULL,
  geom = "hex",
  position = "identity",
  ...,
  bins = 30,
  binwidth = NULL,
  drop = TRUE,
  fun = "mean",
  fun.args = list(),
  na.rm = FALSE,
  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))。 - geom
 - 
用於顯示數據的幾何對象,可以作為
ggprotoGeom子類,也可以作為命名去除geom_前綴的幾何對象的字符串(例如"point"而不是"geom_point") - position
 - 
位置調整,可以是命名調整的字符串(例如
"jitter"使用position_jitter),也可以是調用位置調整函數的結果。如果需要更改調整設置,請使用後者。 - ...
 - 
其他參數傳遞給
layer()。這些通常是美學,用於將美學設置為固定值,例如colour = "red"或size = 3。它們也可能是配對的 geom/stat 的參數。 - bins
 - 
給出垂直和水平方向的 bin 數量的數值向量。默認設置為 30。
 - binwidth
 - 
給出垂直和水平方向 bin 寬度的數值向量。如果兩者均設置,則覆蓋
bins。 - drop
 - 
如果
fun的輸出是NA則丟棄。 - fun
 - 
函數進行總結。
 - fun.args
 - 
要傳遞給
fun的額外參數列表 - na.rm
 - 
如果
FALSE,則默認缺失值將被刪除並帶有警告。如果TRUE,缺失值將被靜默刪除。 - show.legend
 - 
合乎邏輯的。該層是否應該包含在圖例中?
NA(默認值)包括是否映射了任何美學。FALSE從不包含,而TRUE始終包含。它也可以是一個命名的邏輯向量,以精細地選擇要顯示的美學。 - inherit.aes
 - 
如果
FALSE,則覆蓋默認美學,而不是與它們組合。這對於定義數據和美觀的輔助函數最有用,並且不應繼承默認繪圖規範的行為,例如borders()。 
計算變量
這些是由層的 'stat' 部分計算的,可以使用 delayed evaluation 訪問。
- 
after_stat(x),after_stat(y)
地點。 - 
after_stat(value)
匯總統計量的值。 
也可以看看
stat_summary_hex() 用於六邊形摘要。 stat_bin2d() 用於分箱選項。
例子
d <- ggplot(diamonds, aes(carat, depth, z = price))
d + stat_summary_2d()
# Specifying function
d + stat_summary_2d(fun = function(x) sum(x^2))
d + stat_summary_2d(fun = ~ sum(.x^2))
d + stat_summary_2d(fun = var)
d + stat_summary_2d(fun = "quantile", fun.args = list(probs = 0.1))
if (requireNamespace("hexbin")) {
d + stat_summary_hex()
d + stat_summary_hex(fun = ~ sum(.x^2))
}
相關用法
- R ggplot2 stat_summary 總結唯一/分箱 x 處的 y 值
 - R ggplot2 stat_sf_coordinates 從“sf”對象中提取坐標
 - R ggplot2 stat_ellipse 計算法行數據橢圓
 - R ggplot2 stat_identity 保留數據原樣
 - R ggplot2 stat_unique 刪除重複項
 - R ggplot2 stat_ecdf 計算經驗累積分布
 - R ggplot2 scale_gradient 漸變色階
 - R ggplot2 scale_shape 形狀比例,又稱字形
 - R ggplot2 scale_viridis 來自 viridisLite 的 Viridis 色標
 - R ggplot2 scale_grey 連續灰度色階
 - R ggplot2 scale_linetype 線條圖案的比例
 - R ggplot2 scale_discrete 離散數據的位置尺度
 - R ggplot2 scale_manual 創建您自己的離散尺度
 - R ggplot2 scale_colour_discrete 離散色階
 - R ggplot2 scale_steps 分級漸變色標
 - R ggplot2 should_stop 在示例中用於說明何時應該發生錯誤。
 - R ggplot2 scale_size 麵積或半徑比例
 - R ggplot2 scale_date 日期/時間數據的位置刻度
 - R ggplot2 scale_continuous 連續數據的位置比例(x 和 y)
 - R ggplot2 scale_binned 用於對連續數據進行裝箱的位置比例(x 和 y)
 - R ggplot2 sec_axis 指定輔助軸
 - R ggplot2 scale_alpha Alpha 透明度比例
 - R ggplot2 scale_colour_continuous 連續色標和分級色標
 - R ggplot2 scale_identity 使用不縮放的值
 - R ggplot2 scale_linewidth 線寬比例
 
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Bin and summarise in 2d (rectangle & hexagons)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
