title: "Error: Aesthetics must be either length 1 or the same as the data" Why?
body: <p>Wickham (2009: 164-6) gives an example of plotting multiple time series simultaneously. The following code replicates his example:</p>
  
  <pre><code>  range01 &lt;- function(x) {
        rng &lt;- range(x, na.rm = TRUE)
        (x - rng[1])/diff(rng)
    }
  
    emp &lt;- subset(economics_long, variable %in% c("uempmed", "unemploy"))
    emp2 &lt;- ddply(emp, .(variable), transform, value = range01(value))
    qplot(date, value, data = emp2, geom = "line", color = variable, linetype =     variable)
    # Produces a plot that looks like the one on p. 166 of the ggplot2 book.
  </code></pre>
  
  <p>Here range01 is used to recode variables to values within [0,1] so series with different orders of magnitude can be plotted on identical scales. Wickham's original also starts with the <code>employment</code> data provided with ggplot2 and melts it into long form, but here I've taken the shortcut of starting with the <code>employment_long</code> version.</p>
  
  <p>But Wickham (p. 27) also points out that tapping the "full power" of ggplot2 requires manual construction of plots by layers, using the <strong>ggplot()</strong> function. Here is his example again but using <strong>ggplot()</strong> instead of <strong>qplot()</strong>:</p>
  
  <pre><code># Now do the same thing using ggplot commands only
  ggplot(data = emp2, aes(x = date)) +
    geom_line(aes(y = value, group = variable, color = variable, linetype = variable))
  # Get the same results
  </code></pre>
  
  <p>Both examples take advantage of <strong>ggplot2</strong>'s default settings. But suppose we want greater control over the aesthetics. Perhaps some variables lend themselves to particular color schemes (e.g., green might be used for environmentally friendly variables and black, for detrimental ones); or perhaps in a long monograph with many plots we just want to ensure consistency. Furthermore, if the plots will be used both in presentations and printed black-and-white text, we may also want to associate specific line types with particular series; this could also be the case if we are concerned about viewers with color blindness. Finally, variable names usually are poor descriptors of what variables really are, so we want to associate variable labels with the individual time series.</p>
  
  <p>So we define the following for the economics dataset:</p>
  
  <pre><code># Try to control the look a bit more
  economics_colors = c("pce" = "red", "pop" = "orange", "psavert" = "yellow",
      "uempmed" = "green", "unemploy" = "blue")
  economics_linetypes = c("pce" = "solid", "pop" = "dashed", "psavert" = "dotted",
      "uempmed" = "dotdash", "unemploy" = "longdash")
  economics_labels = c(
      "pce" = "Personal consumption expenditures",
      "pop" = "Total population",
      "psavert" = "Personal savings rate",
      "uempmed" = "Median duration of unemployment",
      "unemploy" = "Number of unemployed"
  )
  </code></pre>
  
  <p>Now construct the plot by adding separate layers (Wickham 2009: 164-5) for each variable:</p>
  
  <pre><code># First do it line-by-line
  employment.plot &lt;- ggplot(emp2) + aes(x = date)  +
    scale_linetype_manual(values = economics_linetypes, labels = economics_labels)
  employment.plot &lt;- employment.plot +
    geom_line(data = subset(emp2, variable == "uempmed"),
        aes(y = value, linetype = "uempmed"), color = economics_colors["uempmed"])
  employment.plot &lt;- employment.plot +
    geom_line(data = subset(emp2, variable == "unemploy"),
              aes(x = date, y = value, linetype = "unemploy"), color = economics_colors["unemploy"])
  employment.plot
  # Except for the specific line colors, produces the same plot as before.
  </code></pre>
  
  <p>Notice two things here. First, line types are <strong>mapped</strong> but colors are <strong>set</strong> (see Wickham 2009: 47-49). This produces the desired result of a single legend with distinct color-linetype combinations for each series.</p>
  
  <p>Second, even though the data are organized in "long" format, we used <strong>subset</strong> to select out individual series. This is not the best solution. As Wickham (164-5) says:</p>
  
  <blockquote>
    <p>... a better alternative is to melt the data into a long format and
    then visualize that. In the molten data the time series have their
    value stored in the <strong>value</strong> variable and we can distinguish between
    them with the <strong>variable</strong> variable.</p>
  </blockquote>
  
  <p>So let's try this approach:</p>
  
  <pre><code># Now try it the automatic way
  employment.plot &lt;- ggplot(data = emp2, aes(x = date))  +
    scale_linetype_manual(values = economics_linetypes, labels = economics_labels)
  employment.plot &lt;- employment.plot +
    geom_line(aes(y = value, group = variable, linetype = economics_linetypes), color = economics_colors)
  employment.plot
  # Throws "Error: Aesthetics must be either length 1 or the same as the data (1148) ..."
  </code></pre>
  
  <p>As the comment indicates, this code throws an error regarding the aesthetics. Why?</p>
  
  <p>Also, is there another way to accomplish the multiple goals of using melted data with the single <strong>variable</strong> variable triggering separate lines, controlling which colors and line types are associated with each series, and using code to standardize such conventions across multiple plots?</p>
  
  <h1>References</h1>
  
  <p>Wickham, Hadley. 2009. <em>ggplot2: Elegant Graphics for Data Analysis</em>. Springer.</p>