在 r 中生成带有时间序列数据帧的 ggplot 图

produce ggplot figure with time series data frame in r

我的数据集如下所示:

   Year  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1   1865 -0.6 -1.2  0.2 -0.2 -0.4  0.0  0.5  1.5  1.8 -2.0 -0.9  0.8
2   1866  0.5  0.8 -0.6 -2.3 -2.0  0.9 -0.5 -0.2  2.4 -0.3 -0.5  0.2
3   1867 -3.5  1.1 -4.3  1.8 -4.2  0.1 -2.0  1.9  1.4  2.2 -3.5 -0.1
4   1868  0.7  3.0  3.6  1.7  2.3  3.1  0.4  1.5 -2.8  3.5 -1.8 -0.1
5   1869  1.0  2.5  0.2 -0.2 -2.7 -1.9 -0.3 -1.0 -0.4 -1.4  1.4  0.0
6   1870  0.4 -2.8 -2.0  1.1  1.5  0.3  0.5 -3.6  0.2  0.9 -2.1 -3.0
7   1871  2.0 -0.3  0.9 -2.6 -1.5 -0.9  0.8  0.0 -2.2  0.2 -3.1  1.1
8   1872  1.0 -0.7 -1.1 -0.5  1.0  0.2 -2.3 -2.4 -1.8  1.6  0.4 -0.5
9   1873  1.3 -0.6  1.2 -3.3 -0.9  1.9 -0.1  3.1 -2.2  1.7 -1.9  0.6
10  1874  1.5  0.9  0.9  2.0 -1.3  0.1  0.6  0.9  1.4  1.8 -0.2  0.1
11  1875 -0.7 -1.0 -0.8 -1.8  2.4  3.2 -1.1 -1.6 -1.5  0.1 -3.1 -0.6
12  1876  0.8 -0.7  0.5 -0.6  0.8  3.3  1.7  0.3 -3.7 -1.9 -4.4 -2.1
13  1877  0.9  1.1  0.1 -3.1 -2.8 -0.1  1.3 -5.5 -3.2  0.0  2.2  2.5
14  1878  0.5 -0.2  0.2 -2.7 -0.8 -1.2  0.4 -3.5  1.0 -1.4 -3.1 -5.5
15  1879 -0.9  0.9  1.2 -0.2  1.6 -1.4 -0.4 -0.7  2.9 -0.3 -3.9 -1.2
16  1880 -0.8  1.0  0.5  1.3 -0.6 -0.2 -3.6 -0.4 -1.1 -4.7  1.0 -2.2
17  1881 -5.9 -0.9 -2.3 -1.9  0.9  1.2  1.7 -0.9 -2.2 -0.9  1.4  2.7
18  1882  1.0  0.6  3.6 -1.3 -1.3  0.1  1.8  0.7  1.6  0.3  1.6 -1.4
19  1883  0.7  2.4 -2.4  1.3  1.3 -2.3 -2.5  1.3 -0.7  2.5  1.9  0.7
20  1884  0.7  0.8  1.3 -2.0  0.3  0.9 -3.0  0.9  1.4  1.4 -1.1  2.1
21  1885 -0.4 -1.8  0.9  1.2 -1.6  1.7  0.3 -4.9  1.9 -0.3 -1.2 -1.3
22  1886  0.3 -0.1 -0.9 -1.2 -1.2  0.8  0.7  3.0 -1.2  0.7  1.6 -0.2
23  1887  1.7  1.1 -1.9 -3.2  0.5 -2.0 -0.3 -2.3 -0.7 -3.3 -1.1 -2.7
24  1888 -1.5 -1.6 -1.5 -0.8 -1.5 -1.7 -1.7 -0.4 -3.0 -3.1  2.2  1.4
25  1889  1.2  0.2 -0.2  1.7  0.5  1.7 -3.0  1.5 -1.2  1.0  1.7  3.1
26  1890  2.3 -0.7  2.0  1.8 -0.6  1.7  1.4  1.1  1.3  0.0  2.7 -0.8
27  1891  0.2  0.9 -1.2 -1.6 -0.8 -4.0 -2.8  0.2  1.6  1.1  0.0  1.7
28  1892  0.5 -1.8 -1.4 -0.6 -2.2 -1.6  0.0 -0.2  2.2 -3.2  0.7 -1.3
29  1893 -2.1  0.8 -0.4 -0.2 -1.2 -2.6  0.2 -2.8  1.7  0.4 -2.3  2.3
30  1894  0.5  3.1  1.7  1.8 -0.8  2.7  1.2  0.3 -4.0 -3.1  2.2  0.6
31  1895 -1.7 -4.9  1.0 -0.7  0.6 -0.4  0.1 -0.5  0.1 -2.8 -0.6 -0.5
32  1896 -0.9  0.9  2.6  2.5  1.2 -0.6  2.3  0.0 -0.1 -2.5 -0.9  1.8
33  1897 -1.5  1.1  1.0  3.6  0.1 -2.6 -1.0  1.4  1.0 -2.1 -0.5  0.6
34  1898  1.2  1.2 -0.5  2.2 -1.1 -1.5 -1.5  1.1 -0.8 -0.9  0.9  0.6
35  1899 -1.3 -1.6 -2.1 -0.3 -1.4  1.4  2.0 -2.6  1.4 -2.0  0.6 -0.9
36  1900  1.8 -3.1 -2.1  0.8  0.7 -0.7 -1.0  0.5  2.3 -0.3  2.2  1.2

(从here下载)

我想制作一个与此类似的图表。

我的问题是:使用这种格式的数据集以生成所需图形的最佳方法是什么?最好使用melt吗?例如:

dd <- melt(d, id = "Year")

然后画图

ggplot(dd, aes(x = Year, y = value)) + 
  geom_line()

我本以为数据最初的格式足以绘制此图?

这是一种方法。有必要首先重塑您的数据。 mo2Num 是在流程中创建日期对象的函数。首先,我使用 gather 将您的数据排列在 long-format 中。然后,我使用 mo2Num 将 month(月份名称)更改为 moth(数字)。 date是后面图形中用到的日期对象。 positive 是一列,用于在图形中分配不同的颜色。

library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)

# Credit to Bonded Dust 
mo2Num <- function(x) match(tolower(x), tolower(month.abb))

ana <- gather(mydf, month, value, - Year) %>%
       mutate(month = mo2Num(month),
              date = as.Date(paste(Year, month, "1", sep = "-"), format = "%Y-%m-%d"),
              positive = ifelse(value > 0, 1, 2))

ggplot(data = ana, aes(x = date, y = value, fill = factor(positive))) +
geom_bar(stat = "identity") +
scale_x_date() +
theme(legend.position = "none")

这也可以解决问题:

library("ggplot2")
library("reshape2")

data_long <- melt(data, id.vars = "Year", variable.name = "month", value.name = "nao")
data_long$cat <- ifelse(data_long$nao >= 0, "1", "0")
data_long$time <- with(data_long, as.Date(paste(Year, as.character(month), 1, sep = ""),     format = "%Y%B%d"))

ggplot(data_long, aes(x = time, y = nao, fill = cat)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = c("red", "blue"))

因为您的值基于 Year-Month,您可以将 'Variable' 转换为 class yearmon 并使用包 [=13] 中的 scale_x_yearmon =]:

library(zoo)
library(ggplot2)
library(scales)

dd$ym <- as.yearmon(with(dd, paste(variable, Year)))

ggplot(dd, aes(x = ym, y = value, fill = value < 0)) +
  geom_bar(stat = "identity") +
  scale_x_yearmon()