【问题标题】:plot data with different dates绘制不同日期的数据
【发布时间】:2013-06-28 12:09:18
【问题描述】:

我的数据集的图有些问题。 这是我的数据集的摘录。

   Date      Month Year  Value 1
30/05/96       May 1996 1835
06/12/96  December 1996 1770
18/03/97     March 1997 1640
27/06/97      June 1997 1379
30/09/97 September 1997 1195
24/11/97  November 1997 1335
13/03/98     March 1998 1790
07/05/98       May 1998  349
14/07/98      July 1998 1179
27/10/98   October 1998  665

我想做的是每年针对山 (x) 的值 1 (y) 的图。换句话说,一个带有 3 条线的图,显示了不同年份中每个月 Value 1 的变化。

我执行以下操作:

plot(x[Year==1996,4], xaxt="n")
par(new=T)
plot(x[Year==1997,4], xaxt="n")
axis(1, at=1:length(x$Month), labels=x$Month)

问题是1996的第一个值是指5月,1997的第一个值是指三月。因此,绘制的值是混合的,不再对应于它们的月份。 有没有办法将所有这些值绘制在同一张图中,保持数据的原始对应关系?

【问题讨论】:

    标签: r plot axis


    【解决方案1】:
    df <- read.table(text="Date      Month Year  Value1
    30/05/96       May 1996 1835
    06/12/96  December 1996 1770
    18/03/97     March 1997 1640
    27/06/97      June 1997 1379
    30/09/97 September 1997 1195
    24/11/97  November 1997 1335
    13/03/98     March 1998 1790
    07/05/98       May 1998  349
    14/07/98      July 1998 1179
    27/10/98   October 1998  665", header=T, as.is=T)
    
    df$Month <- factor(df$Month, levels=month.name, ordered=T)
    
    library(ggplot2)
    ggplot(df) + geom_line(aes(Month, Value1, group=Year)) +
      facet_grid(Year~.)
    

    【讨论】:

    • 数据转换和绘图的不错选择。
    • @agstudy 我同意。我刚刚关注了 OP 问题a plot with Value 1 (y) against the mount (x) for every year. In other words, a plot with 3 lines... 他要求 3 行。 @matteo 我说的对吗?
    【解决方案2】:

    还有一个使用@Michele df 的lattice 替代方案。我在这里展示了 2 个替代方案(有和没有刻面)

    library(lattice)
    library(gridExtra)
    p1 <- xyplot(Value1~Month,groups=Year,data=df,
           type=c('p','l'),auto.key=list(columns=3,lines=TRUE))
    p2 <- xyplot(Value1~Month|Year,groups=Year,data=df,layout= c(1,3),
           type=c('p','l'),auto.key=list(columns=3,lines=TRUE))
    grid.arrange(p1,p2)
    

    【讨论】:

    • 我可以问一个简单的问题吗?我使用ggplot2 进行报告,您是否建议尝试lattice(从未使用过)?我的意思是,在你的工作中,你有没有遇到过lattice 能够提供更多东西的情况? (在外观和情节观众易于理解方面)
    • @Michele 您应该考虑每个系统的最强点。 ggplot2 非常好讲一个关于数据的故事:他强大的 aes 系统和 .相当于 aes 形状。尺寸?在格子里看到。还有stat_summary也(很难找到一个格子等价物)在另一边lattice系统比ggplot2更简单(场景后面的视口更少)所以它更灵活更容易定制(一切都可以通过一个函数来定制) ;很容易在lattice panel 中调用grid 函数,我认为lattice 也更快......我不能在这里写更多
    • 没关系,别担心。非常感谢!
    • lattice 是否允许更改 strip.text(正如他们在 ggplot 中命名的那样),或者您只是更喜欢保留颜色(以及传说)?
    • @Michele 这是这里的默认选择(懒惰的)。但是请参阅 this answer 了解如何自定义条带。
    【解决方案3】:

    为您的月份创建一个数值:

    x$MonthNum <- sapply(x$Month, function(x) which(x==month.name))
    

    然后使用这些数值进行绘图,但用文字标记轴。

    plot(NA, xaxt="n", xlab="Month", xlim=c(0,13),
        ylim=c(.96*min(x$Value),1.04*max(x$Value)), type="l")
    z <- sapply(1996:1998, function(y) with(x[x$Year==y,], lines(MonthNum, Value1)))
    axis(1, at=1:12, labels=month.name)
    

    还有一些标签,如果你想识别年份:

    xlabpos <- tapply(x$MonthNum, x$Year, max)
    ylabpos <- mapply(function(mon, year) x$Value1[x$MonthNum==mon & x$Year==year],     
        xlabpos, dimnames(xlabpos)[[1]])
    text(x=xlabpos+.5, y=ylabpos, labels=dimnames(xlabpos)[[1]])
    

    也可以使用layout 获得类似于ggplot 示例的内容:

    par(mar=c(2,4,1,1))
    layout(matrix(1:3))
    z <- sapply(1996:1998, function(y) {
        with(x[x$Year==y,], plot(Value1 ~ MonthNum, xaxt="n", xlab="Month", ylab=y,
            xlim=c(0,13), ylim=c(.96*min(x$Value),1.04*max(x$Value)), type="l"))
        axis(1, at=1:12, labels=month.name)
    })
    

    【讨论】:

    • 感谢您的快速回复。我按照你说的做了,但不幸的是,第二和第三部分(所以 1997 和 1998)没有被绘制出来。
    • @matteo 试过我的吗?不幸的是,我无法上传情节的图片,该死的:-)
    • 哦,现在我可以看到图像了,可能是系统的临时问题(?)
    • 对不起,你是怎么区分年份的?我认为您需要像@agstudy 上图那样的传奇
    • 哦,是的,同意,我刚刚添加了它们。只需编写代码以编程方式处理这些数据。
    猜你喜欢
    • 2015-02-20
    • 2020-09-25
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多