title: plot/ggplot2 - Fill area with too many points
body: <p><strong>Final implementation - not finished but heading the right way</strong></p>
  
  <p><strong>Idea/Problem:</strong> You have a plot with many overlapping points and want to replace them by a plain area, therefore increasing performance viewing the plot.</p>
  
  <p><strong>Possible implementation:</strong> Calculate a distance matrix between all points and connect all points below a specified distance.</p>
  
  <p><strong>Todo/Not finished:</strong> This currently works for manually set distances depending on size of the printed plot. I stopped here because the outcome didnt meet my aesthetic sense.</p>
  
  <p><strong>Minimal example with intermediate plots</strong></p>
  
  <pre><code>set.seed(074079089)
  n.points &lt;- 3000
  
  mat &lt;- matrix(rnorm(n.points*2, 0,0.2), nrow=n.points, ncol=2)
  colnames(mat) &lt;- c("x", "y")
  
  d.mat &lt;- dist(mat)
  fit.mat &lt;-hclust(d.mat, method = "single")
  lims &lt;- c(-1,1)
  real.lims &lt;- lims*1.1               ## ggplot invokes them approximately
  
  # An attempt to estimate the point-sizes, works for default pdfs pdf("test.pdf")
  cutsize &lt;- sum(abs(real.lims))/100  
  groups &lt;- cutree(fit.mat, h=cutsize) # cut tree at height cutsize
  # plot(fit.mat) # display dendogram
  
  # draw dendogram with red borders around the 5 clusters
  # rect.hclust(fit.mat, h=cutsize, border="red")
  
  library(ggplot2)
  df &lt;- data.frame(mat)
  df$groups &lt;- groups
  plot00 &lt;- ggplot(data=df, aes(x,y, col=factor(groups))) + 
      geom_point() + guides(col=FALSE) +  xlim(lims) + ylim(lims)+ 
      ggtitle("Each color is a group")
  pdf("plot00.pdf")
  print(plot00)
  dev.off()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/JauBW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JauBW.png" alt="plot00 - points with group color"></a></p>
  
  <pre><code># If less than 4 points are connected, show them seperately
  t.groups &lt;- table(groups)   # how often which group
  drop.group &lt;- as.numeric(names(t.groups[t.groups&lt;4]))   # groups with less than 4 points are taken together
  groups[groups %in% drop.group] &lt;- 0                     # in group 0
  df$groups &lt;- groups
  plot01 &lt;- ggplot(data=df, aes(x,y, col=factor(groups))) + 
      geom_point() + xlim(lims)+ ylim(lims) + 
      scale_color_hue(l=10)
  pdf("plot01.pdf")
  print(plot01)
  dev.off()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/whkOZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/whkOZ.png" alt="plot01 - all single points in one group"></a></p>
  
  <pre><code>find_hull &lt;- function(df_0) 
  {
      return(df_0[chull(df_0$x, df_0$y), ])
  }
  
  
  library(plyr)
  single.points.df &lt;- df[df$groups == 0 , ]
  connected.points.df &lt;- df[df$groups != 0 , ]
  hulls &lt;- ddply(connected.points.df, "groups", find_hull) #  for all groups find a hull
  plot02 &lt;- ggplot() + 
      geom_point(data=single.points.df, aes(x,y, col=factor(groups))) + 
      xlim(lims)+ ylim(lims) + 
      scale_color_hue(l=10)
  pdf("plot02.pdf")
  print(plot02)
  dev.off()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/58vFV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/58vFV.png" alt="plot02 - only &quot;single&quot;-points (less than 4 connected points)"></a></p>
  
  <pre><code>plot03 &lt;- plot02
  for(grp in names(table(hulls$groups)))
  {
      plot03 &lt;- plot03 + geom_polygon(data=hulls[hulls$groups==grp, ],
                                      aes(x,y), alpha=0.4)
  }
  # print(plot03)
  plot01 &lt;- plot01 + theme(legend.position="none")
  plot03 &lt;- plot03 + theme(legend.position="none")
  # multiplot(plot01, plot03, cols=2)
  pdf("plot03.pdf")
  print(plot03)
  dev.off()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2Eadb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Eadb.png" alt="plot03 - final"></a></p>
  
  <p><strong>Initial Question</strong></p>
  
  <p>I have a (maybe odd) question.</p>
  
  <p>In some plots, I have thousands of points in my analysis. To display them, the pc takes quite a bit of time because there are so many points. 
  After now, many of these points can overlap, I have a filled area (which is fine!). 
  To save time/effort displaying, it would be usefull to just fill this area but plotting each point on its own. </p>
  
  <p>I know there are possibilities in heatmaps and so on, but this is not the idea I have in mind. My idea is something like:</p>
  
  <pre><code>#plot00: ggplot with many many points and a filled area of points
  plot00 &lt;- plot00 + fill.crowded.areas()
  
  # with plot(), I sadly have an idea how to manage it
  </code></pre>
  
  <p>Any ideas? Or is this nothing anyone would do anytime?</p>
  
  <pre><code># Example code
  # install.packages("ggplot2")
  library(ggplot2)
  
  n.points &lt;- 10000
  mat &lt;- matrix(rexp(n.points*2), nrow=n.points, ncol=2)
  colnames(mat) &lt;- c("x", "y")
  df &lt;- data.frame(mat)
  plot00 &lt;- ggplot(df, aes(x=x, y=y)) + 
      theme_bw()  +                       # white background, grey strips
      geom_point(shape=19)# Aussehen der Punkte
  
  print(plot00)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/7WYiY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7WYiY.png" alt="ggplot2"></a></p>
  
  <pre><code># NO ggplot2
  plot(df, pch=19)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/GBI0Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GBI0Y.png" alt="plot "></a></p>
  
  <p><strong>Edit:</strong><br>
  To have density-plots like mentioned by fdetsch (how can I mark the name?) there are some questions concerning this topic. But this is not the thing I want exactly. I know my concern is a bit strange, but the densities make a plot more busy sometimes as necessary.</p>
  
  <p>Links to topics with densities:</p>
  
  <p><a href="https://stackoverflow.com/questions/7714677/r-scatterplot-with-too-many-points">Scatterplot with too many points</a><br>
  <a href="http://blog.trenthauck.com/posts/high-density-scatter-plots/" rel="nofollow noreferrer">High Density Scatter Plots</a></p>