<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/venn-colors-happy-bday-john-venn/"></div></div><div><a data-url="https://www.r-bloggers.com/venn-colors-happy-bday-john-venn/" data-counturl="https://www.r-bloggers.com/venn-colors-happy-bday-john-venn/" data-count="horizontal" data-text="Venn Colors: Happy Bday, John Venn" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://data-steve.github.io/venn-colors-happy-bday-john-venn/"> data_steve</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="https://i2.wp.com/data-steve.github.io/images/venn.png?w=456" data-recalc-dims="1"></p>
<p><a target="_blank" class="twitter-share-button" data-via="data_steve" data-size="large" data-hashtags="rstats, dataviz, datascience" data-dnt="true" href="https://twitter.com/share">Tweet</a></p>
<p>I know that venn diagrams are not as sexy as they used to be and that John Venn’s birthday was <a target="_blank" href="https://flowingdata.com/2014/08/04/google-doodle-venn-diagram/">actually Aug 4</a>, but venn diagram’s still serve a purpose here and there, especially if we can get some more interesting colors in there.</p>
<p>Plus, this question on <a target="_blank" href="https://stat.ethz.ch/pipermail/r-help/2010-October/255807.html">how to hack the venneuler color palette</a> appears to have gone unanswered for a couple years.</p>
<p>The <code>venneuler::venneuler</code> function doesn’t come with a color option directly accessible. However, venneuler package has an unexported plot method <code>venneuler:::plot.VennDiagram</code> that does have a color option that takes hex colors.</p>
<p>The trick is to match the sequencing the labels that comes out of venneuler in the outputted object <code>vv</code> to the colors you want for each label. I hack that below by using <code>match(vv$labels, names(colors))</code>. This allows you to get consistent results on coloring and labels across uses.</p>
<div>
<pre><code># define labels
labels &lt;- c(&quot;Happy&quot;, &quot;Bday&quot;)
# define colors
colors &lt;- c(&quot;#7709d2&quot;,&quot;#64D209&quot;)
# name colors with labels
names(colors) &lt;- labels

# some toy data    IMPORTANT PART IS THAT 
mat &lt;- matrix(c(1880,270,270,470), nrow=2, ncol=2)
colnames(mat) &lt;- labels
rownames(mat) &lt;- labels



make_venn &lt;- function(dat, title=&quot;&quot;,fill_cols=NULL
                      , alpha=.7, title_font=1){
  vv &lt;- venneuler::venneuler(dat)
  if (!is.null(fill_cols)){
    plot(vv, main=title, font.main = title_font
    , col=fill_cols[match(vv$labels, names(colors))], alpha=alpha )
  } else {
    plot(vv, main=title, font.main = title_font, alpha=alpha)
  }
}


make_venn(make_dat(mat),title=&quot;Happy Birthday Venn!&quot;
            ,fill_cols=colors, title_font=2)
</code></pre>
</div>
<p>The key to selecting colors in the venneuler is the thinking in terms of the labels and colors, but the venneuler package expects data have labels that include the co-occurence of the underlying labels:, e.g. <code>c(Happy=1880, Bday=470, &quot;Happy&amp;Bday&quot;=270)</code>.</p>
<p>This presents a slight problem when we’ve only specified the colors for the labels and not their co-occurence. To handle the conversion of a matrix to the structure venneuler expects, I’ve written a helper function <code>make_dat</code>. The function tests if the dat passed in is a matrix or a vector, if it is a vector whether the names contain the required <code>&amp;</code> character.</p>
<div>
<pre><code>make_dat &lt;- function(dat){
 if (is.matrix(dat)) {
      # self-datcuring
      dg &lt;- diag(dat)
    
      # co-tags
      nm &lt;-  nn &lt;-  matrix(nrow=nrow(dat),ncol=nrow(dat))
    
    
      # get names and values from lower.tri
      for (i in seq_along(rownames(dat))){
        for (j in seq_along(rownames(dat))){
          if (i!=j) nm[i,j] &lt;- paste(rownames(dat)[i],rownames(dat)[j], sep=&quot;&amp;&quot;)
          if (i!=j) nn[i,j] &lt;- dat[i,j]
        }
      }
      nn &lt;- na.omit(as.vector(nn))
      nm &lt;- na.omit(as.vector(nm))
      names(nn) &lt;- nm
      c(dg,nn)
  } else if (any(grepl(&quot;&amp;&quot;,names(dat)))){
      dat
  } else stop(&quot;Data format of dat object is incorrect\n\tEither is not a matrix or\n\tnames do not contain '&amp;' for co-tags&quot; )
}
</code></pre>
</div>
<p>Happy Venn diagramming!</p>
<p><a target="_blank" class="twitter-share-button" data-via="data_steve" data-size="large" data-hashtags="rstats, dataviz, datascience" data-dnt="true" href="https://twitter.com/share">Tweet</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/venn-colors-happy-bday-john-venn/"></div></div><div><a data-url="https://www.r-bloggers.com/venn-colors-happy-bday-john-venn/" data-counturl="https://www.r-bloggers.com/venn-colors-happy-bday-john-venn/" data-count="vertical" data-text="Venn Colors: Happy Bday, John Venn" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://data-steve.github.io/venn-colors-happy-bday-john-venn/"> data_steve</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fMXeIJqKOBI"></a></div>