準備資料以進行繪圖

ggplot2 適用於長資料幀。以下樣本資料代表 20 個不同日期的糖果價格,格式描述為寬,因為每個類別都有一列。

set.seed(47)
sweetsWide <- data.frame(date      = 1:20,
                         chocolate = runif(20, min = 2, max = 4),
                         iceCream  = runif(20, min = 0.5, max = 1),
                         candy     = runif(20, min = 1, max = 3))

head(sweetsWide)
##   date chocolate  iceCream    candy
## 1    1  3.953924 0.5890727 1.117311
## 2    2  2.747832 0.7783982 1.740851
## 3    3  3.523004 0.7578975 2.196754
## 4    4  3.644983 0.5667152 2.875028
## 5    5  3.147089 0.8446417 1.733543
## 6    6  3.382825 0.6900125 1.405674

要將 sweetsWide 轉換為長格式以與 ggplot2 一起使用,可以使用基本 R 和包 reshape2data.tabletidyr(按時間順序)的幾個有用函式:

# reshape from base R
sweetsLong <- reshape(sweetsWide, idvar = 'date', direction = 'long', 
                      varying = list(2:4), new.row.names = NULL, times = names(sweetsWide)[-1])

# melt from 'reshape2'
library(reshape2)
sweetsLong <- melt(sweetsWide, id.vars = 'date')

# melt from 'data.table'
# which is an optimized & extended version of 'melt' from 'reshape2'
library(data.table)
sweetsLong <- melt(setDT(sweetsWide), id.vars = 'date')

# gather from 'tidyr'
library(tidyr)
sweetsLong <- gather(sweetsWide, sweet, price, chocolate:candy)

所有都給出了類似的結果:

head(sweetsLong)
##   date     sweet    price
## 1    1 chocolate 3.953924
## 2    2 chocolate 2.747832
## 3    3 chocolate 3.523004
## 4    4 chocolate 3.644983
## 5    5 chocolate 3.147089
## 6    6 chocolate 3.382825

有關在格式和格式之間轉換資料的詳細資訊,另請參閱在格式和格式之間重新整形資料 。 ** **

由此產生的 sweetsLong 有一列價格和一列描述甜味的型別。現在繪圖更簡單:

library(ggplot2)
ggplot(sweetsLong, aes(x = date, y = price, colour = sweet)) + geom_line()

StackOverflow 文件