0

My head is getting sore from me banging it so much.

I have a time-series that I've converted into an xts object w/ 7 variables. Now I'm trying to plot 4 of them, all price indices, on the same graph. I used autoplot (from the ggfortify package) to initialize the graph, and this is where the trouble begins.

Autoplot doesn't seem to work unless I give it at least one variable to plot. That's fine, but the two customizations I want for the variable -- its color and line type -- seem to have no effect.

But once I create the plot this way, I have little trouble adding the other 3 variables by adding geom_lines. Here's sort of what the code looks like:

p <- autoplot(foo.xts,xlab="Year",
         ylab="Price Index",
         columns="Variable1",linetype=4) # the linetype accomplishes nothing
p <- p + geom_line(aes(y="Variable2", color="green", linetype="solid"
       # etc. for the other 2 variables
p # The 3 added variables do get the selected colors & line types.

But how can I customize the line for the first variable?

Then there's another problem in that I can't get a legend to appear. Here's how I'm trying to do that:

p <- p + scale_color_discrete(
     name="Price Indices",
     breaks=c("Variable1", "Variable2", "Variable3", "Variable4"),
     labels=c("Index 1", "Index 2", "Index 3", "Index 4"))

This seems to accomplish nothing.

One thing I'd add is that in my various experiments trying to get the legend to work, I've sometimes gotten two sets of keys: one for colors and one for line types. This is obviously not what I'm after.

If someone could help me with this, I'd be forever in your debt!

Gnosos
  • 47
  • 6
  • It's easier to help if you make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by adding sample data as well so we can run the code ourselves. – MrFlick Feb 02 '17 at 17:03
  • @MrFlick Yes, I was wrong. I confused autoplot with another function from base R's time series. `autoplot` is in fact a generic provided by ggplot2 and xts in fact calls `autoplot.zoo` as you suggest. I've removed my comment to not cause any confusion. – hdkrgr Feb 02 '17 at 18:34
  • I didn't include sample data because the data I'm using are proprietary. I felt there was too much of a chance of accidentally changing the situation by straying from the original data. – Gnosos Feb 03 '17 at 14:51

1 Answers1

-1

I spent yesterday away from the computer, and when I returned in the evening fixed the problems. Here's how:

  1. Stopped using autoplot. It's a classic case of hand-holding that throws you over the cliff. In other words, it automatically formats the plot in ways that are difficult (impossible?) to customize. Instead, ggplot makes the initial plot.

  2. Since I'm making a series of plots, moved all the shared features to a separate, preamble section. This section creates a base plot, sets the x-axis variable (the date of the observation), labels the x-axis, and formats its tick marks. It also sets up standardized colors, line styles, and shapes to be used by all the "production" plots.

  3. To set up the standardized elements, it uses scale_color_manual, etc. Each one has to be identical in all respects except those that are unique to its specific aesthetic attribute. E.g., scale_color_manual uses values like "red" whereas scale_linetype_manual uses values like "solid." Each manual setting includes the following elements: legend.title*, values, labels, and guide = guide_legend()*. (Items marked with * must be identical, otherwise you'll get different legends for each one.) For each plot, the actual legend title is first stored in a variable, legend.title, and then used in all the manual scale setting. This way the manual settings can be moved to the common section, but each plot has is own unique title for its legend.

3A. Actually, I was wrong about this. I was thinking LaTeX, where most things are evaluated where they appear at execution time. So a scale_color_manual statement at the start could change later on just by changing the value of legend.title. But in R, things are evaluated sequentially, and changing legend.title after the scale_color_manual statement is executed will have no effect. I worked around this by defining several variables in the preamble (e.g., one with the colors I'm using) and then using these variables in the various source_x_manual statements. This way, the only thing that change is the legend title.

  1. Then each production plot starts by copying the base plot, labeling the y-axis, and then adds the geometric objects that it needs.

This approach has several advantages. 1) It modularizes the plotting so that problems are easier to isolate and solve, and most solved problems in the preamble section are solved for all plots. 2) It standardizes the plots, ensuring that their common features are formatted identically. 3) It reduces each production plot to a few statements; since this is the unique part for each plot, creating a new style of plot becomes relatively easy. 4) The value added by autoplot becomes minimal because this approach, separating shared elements in a preamble, compensates by isolating reusable code and the preamble, once debugged, allows much more fine-grain customization.

If you have any questions, please feel free to ask.

Gnosos
  • 47
  • 6