title: how to use a categorical variable to control pch value in plot
body: <p>I find that I need to use a function to control what character is used in a scatter plot instead of just passing in a categorical variable.  Why is that?  </p>
  
  <p>Here's what I have tried.</p>
  
  <p>Here is what the input file looks like</p>
  
  <pre><code>~/data $ head avgpm25.csv
  "pm25","fips","region","longitude","latitude"
  9.77118522614686,"01003","east",-87.74826,30.592781
  9.99381725284814,"01027","east",-85.842858,33.26581
  10.6886181012839,"01033","east",-87.72596,34.73148
  11.3374236874237,"01049","east",-85.798919,34.459133
  12.1197644686119,"01055","east",-86.032125,34.018597
  10.8278048723489,"01069","east",-85.350387,31.189731
  11.5839280137523,"01073","east",-86.82805,33.527872
  11.2619958748527,"01089","east",-86.588226,34.73079
  9.41442269955754,"01097","east",-88.139667,30.722256
  </code></pre>
  
  <p>here is a transcript of R session</p>
  
  <pre><code>&gt; pollution &lt;- fread("~/Downloads/data//avgpm25.csv")
  &gt; str(pollution)
  Classes ‘data.table’ and 'data.frame':  576 obs. of  5 variables:
   $ pm25     : num  9.77 9.99 10.69 11.34 12.12 ...
   $ fips     : chr  "01003" "01027" "01033" "01049" ...
   $ region   : chr  "east" "east" "east" "east" ...
   $ longitude: num  -87.7 -85.8 -87.7 -85.8 -86 ...
   $ latitude : num  30.6 33.3 34.7 34.5 34 ...
   - attr(*, ".internal.selfref")=&lt;externalptr&gt; 
  </code></pre>
  
  <p>notice that <code>pollution$region</code> is <code>chr</code></p>
  
  <pre><code>&gt; with(pollution, plot(pm25))  # this works, but all points are black
  &gt; with(pollution, plot(pm25, col = region)) # this fails
  Error in plot.xy(xy, type, ...) : invalid color name 'east'
  &gt; with(pollution, plot(pm25, col = as.factor(region))) # this works, points of two region get painted in two different colors
  </code></pre>
  
  <p>And here is the reason for my question.  Why doesn't this work</p>
  
  <pre><code>&gt; with(pollution, plot(pm25, col = as.factor(region), pch = as.factor(region)))
  Error in plot.xy(xy, type, ...) : invalid plotting symbol
  </code></pre>
  
  <p>but defining a function and then passing it in works!</p>
  
  <pre><code>&gt; pch_func &lt;- function(x) { ifelse (x == "east", 1, 2)}
  &gt; with(pollution, plot(pm25, col = as.factor(region), pch = pch_func(region)))
  </code></pre>
  
  <p>Neither does this work</p>
  
  <pre><code>&gt; pollution &lt;- mutate(pollution, region = as.factor(region))
  &gt; with(pollution, plot(pm25, col = region, pch = region))
  Error in plot.xy(xy, type, ...) : invalid plotting symbol
  </code></pre>
  
  <p>Why is this?  Any help would be appreciated.</p>