當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R ggplot2 geom_function 將函數繪製為連續曲線

計算函數並將其繪製為連續曲線。這使得在現有繪圖上疊加函數變得很容易。使用沿 x 軸均勻分布的值的網格調用該函數,並用一條線繪製結果(默認情況下)。

用法

geom_function(
  mapping = NULL,
  data = NULL,
  stat = "function",
  position = "identity",
  ...,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

stat_function(
  mapping = NULL,
  data = NULL,
  geom = "function",
  position = "identity",
  ...,
  fun,
  xlim = NULL,
  n = 101,
  args = list(),
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

參數

mapping

aes() 創建的一組美學映射。如果指定且inherit.aes = TRUE(默認),它將與繪圖頂層的默認映射組合。如果沒有繪圖映射,則必須提供mapping

data

stat_function() 忽略,請勿使用。

stat

用於該層數據的統計變換,可以作為 ggproto Geom 子類,也可以作為命名去掉 stat_ 前綴的統計數據的字符串(例如 "count" 而不是 "stat_count" )

position

位置調整,可以是命名調整的字符串(例如 "jitter" 使用 position_jitter ),也可以是調用位置調整函數的結果。如果需要更改調整設置,請使用後者。

...

其他參數傳遞給 layer() 。這些通常是美學,用於將美學設置為固定值,例如 colour = "red"size = 3 。它們也可能是配對的 geom/stat 的參數。

na.rm

如果 FALSE ,則默認缺失值將被刪除並帶有警告。如果 TRUE ,缺失值將被靜默刪除。

show.legend

合乎邏輯的。該層是否應該包含在圖例中? NA(默認值)包括是否映射了任何美學。 FALSE 從不包含,而 TRUE 始終包含。它也可以是一個命名的邏輯向量,以精細地選擇要顯示的美學。

inherit.aes

如果 FALSE ,則覆蓋默認美學,而不是與它們組合。這對於定義數據和美觀的輔助函數最有用,並且不應繼承默認繪圖規範的行為,例如borders()

geom

用於顯示數據的幾何對象,可以作為 ggproto Geom 子類,也可以作為命名去除 geom_ 前綴的幾何對象的字符串(例如 "point" 而不是 "geom_point" )

fun

使用的函數。 1) 基本或 rlang 公式語法中的匿名函數(請參閱 rlang::as_function() )或 2) 引用函數的帶引號或字符名稱;請參閱示例。必須矢量化。

xlim

(可選)指定函數的範圍。

n

沿 x 軸插值的點數。

args

傳遞給 fun 定義的函數的附加參數列表。

美學

geom_function() 理解以下美學(所需的美學以粗體顯示):

  • x

  • y

  • alpha

  • colour

  • group

  • linetype

  • linewidth

vignette("ggplot2-specs") 中了解有關設置這些美學的更多信息。

計算變量

這些是由層的 'stat' 部分計算的,可以使用 delayed evaluation 訪問。

  • after_stat(x)
    x沿網格的值。

  • after_stat(y)
    函數的值在相應的位置評估x.

也可以看看

例子


# geom_function() is useful for overlaying functions
set.seed(1492)
ggplot(data.frame(x = rnorm(100)), aes(x)) +
  geom_density() +
  geom_function(fun = dnorm, colour = "red")


# To plot functions without data, specify range of x-axis
base <-
  ggplot() +
  xlim(-5, 5)

base + geom_function(fun = dnorm)


base + geom_function(fun = dnorm, args = list(mean = 2, sd = .5))


# The underlying mechanics evaluate the function at discrete points
# and connect the points with lines
base + stat_function(fun = dnorm, geom = "point")


base + stat_function(fun = dnorm, geom = "point", n = 20)


base + stat_function(fun = dnorm, geom = "polygon", color = "blue", fill = "blue", alpha = 0.5)


base + geom_function(fun = dnorm, n = 20)


# Two functions on the same plot
base +
  geom_function(aes(colour = "normal"), fun = dnorm) +
  geom_function(aes(colour = "t, df = 1"), fun = dt, args = list(df = 1))


# Using a custom anonymous function
base + geom_function(fun = function(x) 0.5*exp(-abs(x)))


base + geom_function(fun = ~ 0.5*exp(-abs(.x)))


# Using a custom named function
f <- function(x) 0.5*exp(-abs(x))

base + geom_function(fun = f)


# Using xlim to restrict the range of function
ggplot(data.frame(x = rnorm(100)), aes(x)) +
geom_density() +
geom_function(fun = dnorm, colour = "red", xlim=c(-1, 1))


# Using xlim to widen the range of function
ggplot(data.frame(x = rnorm(100)), aes(x)) +
geom_density() +
geom_function(fun = dnorm, colour = "red", xlim=c(-7, 7))


相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Draw a function as a continuous curve。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。