Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perhaps you are not using <code>as.yearmon()</code> correctly, because the following works for me (using <code>dat</code> from Gavin's answer):</p> <pre><code>library(zoo) dat$date &lt;- as.yearmon(dat$date, "%YM%m") </code></pre> <p>Thus, working through to getting things to plot correctly:</p> <ol> <li><p>Your data:</p> <pre><code>dat &lt;- read.table(text = "date x x2 1975M1 112.44 113.12 1975M2 113.1 114.36 1975M3 115.04 114.81 1975M4 117.65 115.35 1975M5 119.5 116.92 1975M6 121.4 118.56 1975M7 120.64 118.97 1975M8 119.12 119.84 1975M9 118.91 120.59 1975M10 120.58 122.3 1975M11 121.26 123.35 1975M12 122.34 123.33", header = TRUE) </code></pre></li> <li><p>Conversion to <code>xts</code> using <code>as.yearmon()</code> from the "zoo" package.</p> <pre><code>library(xts) # Will also load zoo dat.xts &lt;- xts(dat[-1], order.by = as.yearmon(dat$date, "%YM%m")) dat.xts # x x2 # Jan 1975 112.44 113.12 # Feb 1975 113.10 114.36 # Mar 1975 115.04 114.81 # Apr 1975 117.65 115.35 # May 1975 119.50 116.92 # Jun 1975 121.40 118.56 # Jul 1975 120.64 118.97 # Aug 1975 119.12 119.84 # Sep 1975 118.91 120.59 # Oct 1975 120.58 122.30 # Nov 1975 121.26 123.35 # Dec 1975 122.34 123.33 </code></pre></li> <li><p>Plotting your data:</p> <pre><code>plot.zoo(dat.xts) </code></pre> <p><img src="https://i.stack.imgur.com/GtRdm.png" alt="enter image description here"></p> <pre><code>plot.zoo(dat.xts, plot.type="single", col = c("red", "blue")) </code></pre> <p><img src="https://i.stack.imgur.com/oZc0S.png" alt="enter image description here"></p></li> </ol> <h1>Update: Specifying your own axes</h1> <p>Here is some sample data to work with (it's usually nice to share such sample data when asking questions on SO since it makes it easier for others to replicate and address your problem(s)). Note that for this example, we've skipped using the "xts" package since it's not really necessary.</p> <pre><code>set.seed(1) dat &lt;- data.frame(date = paste0(rep(1975:1977, each = 12), "M", rep(1:12, times = 3)), x1 = runif(36, min = 100, max = 140), x2 = runif(36, min = 100, max = 140)) library(zoo) # xts is actually unnecessary if this is all you're doing # Convert your data to a `zoo` object dat.z &lt;- zoo(dat[-1], order.by = as.yearmon(dat$date, "%YM%m")) </code></pre> <p>This is the default plot obtained with <code>plot(dat.z, screen = 1, col = 1:2)</code>:</p> <p><img src="https://i.stack.imgur.com/sNOfb.png" alt="enter image description here"></p> <p>From your comments, it sounds like you want something like monthly labels.</p> <ol> <li><p>Plot the data, but suppress the x-axis with <code>xaxt = "n"</code></p> <pre><code>plot(dat.z, screen = 1, col = 1:2, xaxt = "n") </code></pre></li> <li><p>Do some setup work to have a label for every month. (See <code>?plot.zoo</code>, from where this is modified.)</p> <pre><code>tt &lt;- time(dat.z) # The following is just the sequence 1:36. # If you wanted only every third month plotted, # use a sequence like ix &lt;- seq(1, length(tt), 3) ix &lt;- seq_along(tt) # What format do you want for your labels. # This yields abbreviated month - abbreviated year fmt &lt;- "%b-%y" labs &lt;- format(tt, fmt) # Generate the vector of your labels </code></pre></li> <li><p>Add your axis to your plot. Some experimentation might be needed to find the right sizes for everything. <code>las = 2</code> makes the labels perpendicular to the axis, which is required if you <em>really</em> feel the need to include a label for every month of each year.</p> <pre><code>axis(side = 1, at = tt[ix], labels = labs[ix], tcl = -0.7, cex.axis = 0.7, las = 2) </code></pre></li> </ol> <p>Here is the final plot:</p> <p><img src="https://i.stack.imgur.com/7zEH4.png" alt="enter image description here"></p> <p>By the way, if you are getting dates like <code>1977.15</code> and so on, you might want to read through some of the answers <a href="https://stackoverflow.com/questions/10302261/forecasting-time-series-data">to this question</a>, for example, looking at @joran's use of <code>pretty()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload