在X轴和Y轴上绘制日期的时间序列图

时间:2017-06-14 12:13:15

标签: r

我使用R来重现以下图表:Image of a line graph with date and month on the Y axis and the year on the X axis. All points on the graph are connected by a single line.

所以我使用了以下数据框

Doy = as.data.frame(list( Year=Year, M_Day =M_Day, Mon_day = Mon_day))

其中Year,M_Day和Mon_day如下所示

Year = c(1960, 1961,
  1962,
  1963,
  1964,
  1965,
  1966,
  1967,
  1968,
  1969,
  1970,
  1971,
  1972,
  1973,
  1974,
  1975,
  1976,
  1977,
  1978,
  1979
)

M_Day = c("05-14",
  "05-05",
  "05-15",
  "05-31",
  "05-01",
  "05-01",
  "05-01",
  "05-01",
  "05-01",
  "05-01",
  "05-16",
  "05-14",
  "05-09",
  "05-22",
  "05-18",
  "06-17",
  "05-03",
  "05-06",
  "05-01",
  "05-03")

Mon_day = c("May-14",
  "May-05",
  "May-15",
  "May-31",
  "May-01",
  "May-01",
  "May-01",
  "May-01",
  "May-01",
  "May-01",
  "May-16",
  "May-14",
  "May-09",
  "May-22",
  "May-18",
  "Jun-17",
  "May-03",
  "May-06",
  "May-01",
  "May-03")

以及ggplot2的以下两个命令

ggplot( Doy) + geom_line( aes( x=Year, y = M_Day)) + geom_point(aes( x=Year, y = M_Day)) 

ggplot( Doy) + geom_line( aes( x=Year, y = Mon_day)) + geom_point(aes( x=Year, y = Mon_day))

给了我以下内容 Image of two graphs both of which have the date and month on the Y axis and the year on the X axis. Only the points with the same Y value are connected so there are multiple lines on the graph and some points aren't connected by a line at all.

与第一张图不相似。

1 个答案:

答案 0 :(得分:0)

使用as.Date(M_Day, format="%m-%d")将日期转换为日期格式,并在比例中指定所需的日期输出格式:

Year <- 1960:1979

M_Day <- as.Date(x=c("05-14", "05-05", "05-15", "05-31", "05-01", "05-01", "05-01", "05-01", "05-01", "05-01", "05-16", "05-14", "05-09", "05-22", "05-18", "06-17", "05-03", "05-06", "05-01", "05-03"),
                format="%m-%d")

Doy <- data.frame(Year=Year, M_Day=M_Day)

ggplot(Doy) + geom_line(aes(x=Year, y=M_Day), col="darkblue") + 
  geom_point(aes(x=Year, y=M_Day), col="darkred") + 
  scale_y_date(date_breaks = "1 week", date_labels = "%d %b")