开发者

Plot with dates as X axis in R

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?


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜