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


R ggplot2 ggplot 創建一個新的ggplot


ggplot() 初始化 ggplot 對象。它可用於聲明圖形的輸入數據幀,並指定一組在所有後續層中通用的繪圖美學,除非特別覆蓋。

用法

ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame())

參數

data

用於繪圖的默認數據集。如果還不是 data.frame,將由 fortify() 轉換為 data.frame。如果未指定,則必須在添加到繪圖的每個圖層中提供。

mapping

用於繪圖的默認美學映射列表。如果未指定,則必須在添加到繪圖的每個圖層中提供。

...

其他參數傳遞給方法。目前未使用。

environment

在整潔評估之前使用。

細節

ggplot() 用於構造初始繪圖對象,並且後麵幾乎總是跟有一個加號 ( + ) 以將組件添加到繪圖中。

用於調用 ggplot() 的三種常見模式:

  • ggplot(data = df, mapping = aes(x, y, other aesthetics))

  • ggplot(data = df)

  • ggplot()

如果所有圖層都使用相同的數據和相同的美學集,則建議使用第一種模式,盡管在使用來自另一個數據幀的數據添加圖層時也可以使用此方法。

第二個模式指定用於繪圖的默認 DataFrame ,但沒有預先定義美觀。當一個 DataFrame 主要用於繪圖但美觀程度因圖層而異時,這非常有用。

第三種模式初始化一個骨架 ggplot 對象,該對象隨著層的添加而充實。當使用多個數據幀生成不同的圖層時(複雜圖形中經常出現這種情況),這非常有用。

參數中的 data =mapping = 規範是可選的(在實踐中經常被省略),隻要數據和映射值以正確的順序傳遞到函數中即可。然而,在下麵的示例中,為了清楚起見,它們保留在原處。

例子

# Create a data frame with some sample data, then create a data frame
# containing the mean value for each group in the sample data.
set.seed(1)

sample_df <- data.frame(
  group = factor(rep(letters[1:3], each = 10)),
  value = rnorm(30)
)

group_means_df <- setNames(
  aggregate(value ~ group, sample_df, mean),
  c("group", "group_mean")
)

# The following three code blocks create the same graphic, each using one
# of the three patterns specified above. In each graphic, the sample data
# are plotted in the first layer and the group means data frame is used to
# plot larger red points on top of the sample data in the second layer.

# Pattern 1
# Both the `data` and `mapping` arguments are passed into the `ggplot()`
# call. Those arguments are omitted in the first `geom_point()` layer
# because they get passed along from the `ggplot()` call. Note that the
# second `geom_point()` layer re-uses the `x = group` aesthetic through
# that mechanism but overrides the y-position aesthetic.
ggplot(data = sample_df, mapping = aes(x = group, y = value)) +
  geom_point() +
  geom_point(
    mapping = aes(y = group_mean), data = group_means_df,
    colour = 'red', size = 3
  )


# Pattern 2
# Same plot as above, passing only the `data` argument into the `ggplot()`
# call. The `mapping` arguments are now required in each `geom_point()`
# layer because there is no `mapping` argument passed along from the
# `ggplot()` call.
ggplot(data = sample_df) +
  geom_point(mapping = aes(x = group, y = value)) +
  geom_point(
    mapping = aes(x = group, y = group_mean), data = group_means_df,
    colour = 'red', size = 3
  )


# Pattern 3
# Same plot as above, passing neither the `data` or `mapping` arguments
# into the `ggplot()` call. Both those arguments are now required in
# each `geom_point()` layer. This pattern can be particularly useful when
# creating more complex graphics with many layers using data from multiple
# data frames.
ggplot() +
  geom_point(mapping = aes(x = group, y = value), data = sample_df) +
  geom_point(
    mapping = aes(x = group, y = group_mean), data = group_means_df,
    colour = 'red', size = 3
  )

源代碼:R/plot.R

相關用法


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