2017-06-14 125 views
1

我,使用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.剧情一个时间序列图与在X轴和Y轴的日期

因此,我使用了以下的数据帧

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

其中年份,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.

这是不是类似第一张图。

回答

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") 
+0

非常感谢mzuba,这正是我需要的:) :)。 – Andy