title: ggplot K-Means Cluster Centers and Clusters
body: <p>I'm working on a K-Means Cluster <code>ggplot()</code>, and can almost do what I'm hoping to but I just can't figure out how to color my centers the same colors as their respective clusters. </p>
  
  <p>So far I have this:</p>
  
  <pre><code>data(mtcars)
  library(ggplot2)
  c1 &lt;- kmeans(mtcars,9)
  x &lt;- tapply(mtcars$mpg,c1$cluster,mean)
  y &lt;- tapply(mtcars$hp,c1$cluster,mean)
  kcenters &lt;- data.frame(x,y)
  ggplot(mtcars,aes(mpg,hp))+geom_point(col=c1$cluster,size=4) +  geom_point(data=kcenters,aes(x,y),pch=8,size=10)
  </code></pre>
  
  <p>Which gives me this plot:<a href="https://i.stack.imgur.com/tSBir.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tSBir.png" alt="enter image description here"></a></p>
  
  <p>So I have two questions, how can I color my centers the same as the cluster they are representing? Also, I feel as though the <code>x</code> and <code>y</code> code is extra that doesn't need to be there because in my <code>c1</code> values I can see centers with the matrix of location and the color they represent as well. I just haven't been able to figure out how to write the code to access this part as every time I try I get an error such as...</p>
  
  <pre><code>Error: Aesthetics must be either length 1 or the same as the data (9): shape, colour, size
  </code></pre>
  
  <p>The other, less important question is about why I have two different black clusters. Doesn't R have more than 8 unique colors it can call on by itself?</p>