在 R 中以日期为 X 轴绘图

发布于 2024-10-04 22:34:37 字数 2756 浏览 6 评论 0原文

我以电子方式维护我的日记,我试图了解过去几个月我的日记写作的一致性。我有以下数据文件,其中显示了我在过去 30 天内记录的日记条目数 (Entry Count) 和单词数 (Word Count)。

Date    Entry Count Word Count
2010-08-25  22  4205
2010-08-26  21  4012
2010-08-27  20  3865
2010-08-28  20  4062
2010-08-29  19  3938
2010-08-30  18  3759
2010-08-31  17  3564
2010-09-01  17  3564
2010-09-02  16  3444
2010-09-03  17  3647
2010-09-04  17  3617
2010-09-05  16  3390
2010-09-06  15  3251
2010-09-07  15  3186
2010-09-08  15  3186
2010-09-09  16  3414
2010-09-10  15  3228
2010-09-11  14  3006
2010-09-12  13  2769
2010-09-13  13  2781
2010-09-14  12  2637
2010-09-15  13  2774
2010-09-16  13  2808
2010-09-17  12  2732
2010-09-18  12  2664
2010-09-19  13  2931
2010-09-20  13  2751
2010-09-21  13  2710
2010-09-22  14  2950
2010-09-23  14  2834
2010-09-24  14  2834
2010-09-25  14  2834
2010-09-26  14  2834
2010-09-27  14  2834
2010-09-28  14  2543
2010-09-29  14  2543
2010-09-30  15  2884
2010-10-01  16  3105
2010-10-02  16  3105
2010-10-03  16  3105
2010-10-04  15  2902
2010-10-05  14  2805
2010-10-06  14  2805
2010-10-07  14  2805
2010-10-08  14  2812
2010-10-09  15  2895
2010-10-10  14  2667
2010-10-11  15  2876
2010-10-12  16  2938
2010-10-13  17  3112
2010-10-14  16  2894
2010-10-15  16  2894
2010-10-16  16  2923
2010-10-17  15  2722
2010-10-18  15  2722
2010-10-19  14  2544
2010-10-20  13  2277
2010-10-21  13  2329
2010-10-22  12  2132
2010-10-23  11  1892
2010-10-24  10  1764
2010-10-25  10  1764
2010-10-26  10  1764
2010-10-27  10  1764
2010-10-28  10  1764
2010-10-29  9   1670
2010-10-30  10  1969
2010-10-31  10  1709
2010-11-01  10  1624
2010-11-02  11  1677
2010-11-03  11  1677
2010-11-04  11  1677
2010-11-05  11  1677
2010-11-06  12  1786
2010-11-07  12  1786
2010-11-08  11  1529
2010-11-09  10  1446
2010-11-10  11  1682
2010-11-11  11  1540
2010-11-12  11  1673
2010-11-13  11  1765
2010-11-14  12  1924
2010-11-15  13  2276
2010-11-16  12  2110
2010-11-17  13  2524
2010-11-18  14  2615
2010-11-19  14  2615
2010-11-20  15  2706
2010-11-21  14  2549
2010-11-22  15  2647
2010-11-23  16  2874
2010-11-24  16  2874
2010-11-25  16  2874
2010-11-26  17  3249
2010-11-27  18  3421
2010-11-28  18  3421
2010-11-29  19  3647

我正在尝试用 R 绘制这些数据,以获得我的日记写作一致性的图形表示。我使用以下命令将其加载到 R 中。

d <- read.table("journal.txt", header=T, sep="\t")

然后我可以使用以下命令绘制数据图表。

plot(seq(from=1, to=length(d$Entry.Count), by=1), d$Entry.Count, type="o", ylim=c(0, max(d$Entry.Count)))

然而,在此图中,X 轴只是一个数字,而不是日期。我尝试调整命令以在 X 轴上显示日期,如下所示。

plot(d$Date, d$Entry.Count, type="o", ylim=c(0, max(d$Entry.Count)))

然而,不仅该图看起来很奇怪,而且 X 轴上的标签也没有多大帮助。绘制这些数据以便我可以清楚地将日期与绘制曲线上的点关联起来的最佳方法是什么?

I maintain my journal electronically and I'm trying to get an idea of how consistent I've been with my journal writing over the last few months. I have the following data file, which shows how many journal entries (Entry Count) and words (Word Count) I recorded over the preceding 30-day period.

Date    Entry Count Word Count
2010-08-25  22  4205
2010-08-26  21  4012
2010-08-27  20  3865
2010-08-28  20  4062
2010-08-29  19  3938
2010-08-30  18  3759
2010-08-31  17  3564
2010-09-01  17  3564
2010-09-02  16  3444
2010-09-03  17  3647
2010-09-04  17  3617
2010-09-05  16  3390
2010-09-06  15  3251
2010-09-07  15  3186
2010-09-08  15  3186
2010-09-09  16  3414
2010-09-10  15  3228
2010-09-11  14  3006
2010-09-12  13  2769
2010-09-13  13  2781
2010-09-14  12  2637
2010-09-15  13  2774
2010-09-16  13  2808
2010-09-17  12  2732
2010-09-18  12  2664
2010-09-19  13  2931
2010-09-20  13  2751
2010-09-21  13  2710
2010-09-22  14  2950
2010-09-23  14  2834
2010-09-24  14  2834
2010-09-25  14  2834
2010-09-26  14  2834
2010-09-27  14  2834
2010-09-28  14  2543
2010-09-29  14  2543
2010-09-30  15  2884
2010-10-01  16  3105
2010-10-02  16  3105
2010-10-03  16  3105
2010-10-04  15  2902
2010-10-05  14  2805
2010-10-06  14  2805
2010-10-07  14  2805
2010-10-08  14  2812
2010-10-09  15  2895
2010-10-10  14  2667
2010-10-11  15  2876
2010-10-12  16  2938
2010-10-13  17  3112
2010-10-14  16  2894
2010-10-15  16  2894
2010-10-16  16  2923
2010-10-17  15  2722
2010-10-18  15  2722
2010-10-19  14  2544
2010-10-20  13  2277
2010-10-21  13  2329
2010-10-22  12  2132
2010-10-23  11  1892
2010-10-24  10  1764
2010-10-25  10  1764
2010-10-26  10  1764
2010-10-27  10  1764
2010-10-28  10  1764
2010-10-29  9   1670
2010-10-30  10  1969
2010-10-31  10  1709
2010-11-01  10  1624
2010-11-02  11  1677
2010-11-03  11  1677
2010-11-04  11  1677
2010-11-05  11  1677
2010-11-06  12  1786
2010-11-07  12  1786
2010-11-08  11  1529
2010-11-09  10  1446
2010-11-10  11  1682
2010-11-11  11  1540
2010-11-12  11  1673
2010-11-13  11  1765
2010-11-14  12  1924
2010-11-15  13  2276
2010-11-16  12  2110
2010-11-17  13  2524
2010-11-18  14  2615
2010-11-19  14  2615
2010-11-20  15  2706
2010-11-21  14  2549
2010-11-22  15  2647
2010-11-23  16  2874
2010-11-24  16  2874
2010-11-25  16  2874
2010-11-26  17  3249
2010-11-27  18  3421
2010-11-28  18  3421
2010-11-29  19  3647

I'm trying to plot this data with R to get a graphical representation of my journal-writing consistency. I load it into R with the following command.

d <- read.table("journal.txt", header=T, sep="\t")

I can then graph the data with the following command.

plot(seq(from=1, to=length(d$Entry.Count), by=1), d$Entry.Count, type="o", ylim=c(0, max(d$Entry.Count)))

However, in this plot the X axis is just a number, not a date. I tried adjusting the command to show dates on the X axis like this.

plot(d$Date, d$Entry.Count, type="o", ylim=c(0, max(d$Entry.Count)))

However, not only does the plot look strange, but the labels on the X axis are not very helpful. What is the best way to plot this data so that I can clearly associate dates with points on the plotted curve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2024-10-11 22:34:37

根据您的代码,日期只是字符。

尝试将它们转换为日期:

plot(as.Date(d$Date), d$Entry.Count)

在您的情况下非常简单,因为“%Y-%m-%d”格式是 as.Date 的默认格式。有关更通用的选项,请参阅 strptime。

Based on your code the dates are just characters.

Try converting them to Dates:

plot(as.Date(d$Date), d$Entry.Count)

Quite simple in your case as the "%Y-%m-%d" format is the default for as.Date. See strptime for more general options.

挖个坑埋了你 2024-10-11 22:34:37

您可以使用zoo?plot.zoo 有几个如何创建自定义轴标签的示例。

z <- zoo(d[,-1],as.Date(d[,1]))
plot(z)

# Example of custom axis labels
plot(z$Entry.Count, screen = 1, col = 1:2, xaxt = "n")
ix <- seq(1, length(time(z)), 3)
axis(1, at = time(z)[ix], labels = format(time(z)[ix],"%b-%d"), cex.axis = 0.7)

You could use zoo. ?plot.zoo has several examples of how to create custom axis labels.

z <- zoo(d[,-1],as.Date(d[,1]))
plot(z)

# Example of custom axis labels
plot(z$Entry.Count, screen = 1, col = 1:2, xaxt = "n")
ix <- seq(1, length(time(z)), 3)
axis(1, at = time(z)[ix], labels = format(time(z)[ix],"%b-%d"), cex.axis = 0.7)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文