准备数据以进行绘图

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 文档