A ggplot-based Marimekko/Mosaic plot

[This article was first published on That’s so Random, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One of my first baby steps into the open source world, was when I answered this SO question over four years ago. Recently I revisited the post and saw that Z.Lin did a very nice and more modern implementation, using dplyr and facetting in ggplot2. I decided to merge here ideas with mine to create a general function that makes MM plots. I also added two features: counts, proportions, or percentages to the cells as text and highlighting cells by a condition.

For those of you unfamiliar with this type of plot, it graphs the joint distribution of two categorical variables. x is plotted in bins, with the bin widths reflecting its marginal distribution. The fill of the bins is based on y. Each bin is filled by the co-occurence of its x and y values. When x and y are independent, all the bins are filled (approximately) in the same way. The nice feature of the MM plot, is that is shows both the joint distribution and the marginal distributions of x and y.

ggmm

To demonstrate the function, I’ll take a selection of the emergency data set from the padr package. Such that it has three types of incidents in four parts of town. We also do some relabelling for prettier plot labels.

<span class="n">em_sel</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">padr</span><span class="o">::</span><span class="n">emergency</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="w">
  </span><span class="n">title</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Traffic: VEHICLE ACCIDENT -"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Traffic: DISABLED VEHICLE -"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Fire: FIRE ALARM"</span><span class="p">),</span><span class="w">
  </span><span class="n">twp</span><span class="w">   </span><span class="o">%in%</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"LOWER MERION"</span><span class="p">,</span><span class="w"> </span><span class="s2">"ABINGTON"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NORRISTOWN"</span><span class="p">,</span><span class="w"> </span><span class="s2">"UPPER MERION"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">twp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">factor</span><span class="p">(</span><span class="n">twp</span><span class="p">,</span><span class="w"> 
                      </span><span class="n">levels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"LOWER MERION"</span><span class="p">,</span><span class="w"> </span><span class="s2">"ABINGTON"</span><span class="p">,</span><span class="w"> </span><span class="s2">"NORRISTOWN"</span><span class="p">,</span><span class="w"> </span><span class="s2">"UPPER MERION"</span><span class="p">),</span><span class="w">
                      </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Low Mer."</span><span class="p">,</span><span class="w"> </span><span class="s2">"Abing."</span><span class="p">,</span><span class="w"> </span><span class="s2">"Norris."</span><span class="p">,</span><span class="w"> </span><span class="s2">"Upper Mer."</span><span class="p">)))</span><span class="w">
</span>

The function takes a data frame and the bare (unquoted) column names of the x and y variables. It will then create a ggplot object. The variables don’t have to be factors or characters, the function coerces them to character.

<span class="n">ggmm</span><span class="p">(</span><span class="n">em_sel</span><span class="p">,</span><span class="w"> </span><span class="n">twp</span><span class="p">,</span><span class="w"> </span><span class="n">title</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-3

Now I promised you two additional features. First, adding text to the cells. The add_text argument takes either “n”, to show the absolute counts

<span class="n">ggmm</span><span class="p">(</span><span class="n">em_sel</span><span class="p">,</span><span class="w"> </span><span class="n">twp</span><span class="p">,</span><span class="w"> </span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="n">add_text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"n"</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-4

“prop” to show the proportions of each cell with respect to the joint distribution

<span class="n">ggmm</span><span class="p">(</span><span class="n">em_sel</span><span class="p">,</span><span class="w"> </span><span class="n">twp</span><span class="p">,</span><span class="w"> </span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="n">add_text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"prop"</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-5

or “perc”, which reflects the percentages of the joint.

<span class="n">ggmm</span><span class="p">(</span><span class="n">em_sel</span><span class="p">,</span><span class="w"> </span><span class="n">twp</span><span class="p">,</span><span class="w"> </span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="n">add_text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"perc"</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-6

An argument is provided to control the rounding of the text.

Secondly, the alpha_condition argument takes an unevaluated expression in terms of the column names of x and y. The cells for which the expression yields TRUE will be highlighted (or rather the others will be downlighted). This is useful when you want to stress an aspect of the distribution, like a value of y that varies greatly over x.

<span class="n">ggmm</span><span class="p">(</span><span class="n">em_sel</span><span class="p">,</span><span class="w"> </span><span class="n">twp</span><span class="p">,</span><span class="w"> </span><span class="n">title</span><span class="p">,</span><span class="w"> 
     </span><span class="n">alpha_condition</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">title</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Traffic: DISABLED VEHICLE -"</span><span class="p">)</span><span class="w">
</span>

plot of chunk unnamed-chunk-7

I hope you find this function useful. The source code is shared below. Also it is in the package accompanying this blog. Which you can install by running devtools::install_github(EdwinTh/thatssorandom).

<span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">ggmm</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w">
                 </span><span class="n">x</span><span class="p">,</span><span class="w">
                 </span><span class="n">y</span><span class="p">,</span><span class="w">
                 </span><span class="n">alpha_condition</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w">
                 </span><span class="n">add_text</span><span class="w">        </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="s2">"n"</span><span class="p">,</span><span class="w"> </span><span class="s2">"prop"</span><span class="p">,</span><span class="w"> </span><span class="s2">"perc"</span><span class="p">),</span><span class="w">
                 </span><span class="n">round_text</span><span class="w">      </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">stopifnot</span><span class="p">(</span><span class="n">is.data.frame</span><span class="p">(</span><span class="n">df</span><span class="p">))</span><span class="w">
  </span><span class="n">add_text</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">match.arg</span><span class="p">(</span><span class="n">add_text</span><span class="p">)</span><span class="w">

  </span><span class="n">x_q</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w">
  </span><span class="n">y_q</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w">
  </span><span class="n">a_q</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">enquo</span><span class="p">(</span><span class="n">alpha_condition</span><span class="p">)</span><span class="w">

  </span><span class="n">plot_set</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">df</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">add_alpha_ind</span><span class="p">(</span><span class="n">a_q</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">x_cat_y_cat</span><span class="p">(</span><span class="n">x_q</span><span class="p">,</span><span class="w"> </span><span class="n">y_q</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">add_freqs_col</span><span class="p">()</span><span class="w">

  </span><span class="n">plot_return</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">mm_plot</span><span class="p">(</span><span class="n">plot_set</span><span class="p">,</span><span class="w"> </span><span class="n">x_q</span><span class="p">,</span><span class="w"> </span><span class="n">y_q</span><span class="p">)</span><span class="w">

  </span><span class="n">plot_return</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">set_alpha</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w"> </span><span class="n">plot_return</span><span class="p">,</span><span class="w"> </span><span class="n">a_q</span><span class="p">)</span><span class="w">

  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nf">is.na</span><span class="p">(</span><span class="n">add_text</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">plot_set</span><span class="o">$</span><span class="n">text</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">make_text_vec</span><span class="p">(</span><span class="n">plot_set</span><span class="p">,</span><span class="w"> </span><span class="n">add_text</span><span class="p">,</span><span class="w"> </span><span class="n">round_text</span><span class="p">)</span><span class="w">
    </span><span class="n">plot_set</span><span class="o">$</span><span class="n">freq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">calculate_coordinates</span><span class="p">(</span><span class="n">plot_return</span><span class="p">)</span><span class="w">
    </span><span class="n">text_part</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">geom_text</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">plot_set</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">text</span><span class="p">))</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
     </span><span class="n">text_part</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">NULL</span><span class="w">
  </span><span class="p">}</span><span class="w">

  </span><span class="n">plot_return</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">text_part</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">add_alpha_ind</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w"> </span><span class="n">a_q</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">df</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">alpha_ind</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!!</span><span class="n">a_q</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">x_cat_y_cat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w"> </span><span class="n">x_q</span><span class="p">,</span><span class="w"> </span><span class="n">y_q</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">df</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">x_cat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="o">!!</span><span class="n">x_q</span><span class="p">),</span><span class="w">
                  </span><span class="n">y_cat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="o">!!</span><span class="n">y_q</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">add_freqs_col</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">stopifnot</span><span class="p">(</span><span class="nf">all</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s1">'x_cat'</span><span class="p">,</span><span class="w"> </span><span class="s1">'y_cat'</span><span class="p">,</span><span class="w"> </span><span class="s1">'alpha_ind'</span><span class="p">)</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="n">colnames</span><span class="p">(</span><span class="n">df</span><span class="p">)))</span><span class="w">
  </span><span class="n">df</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">group_by</span><span class="p">(</span><span class="n">x_cat</span><span class="p">,</span><span class="w"> </span><span class="n">y_cat</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">summarise</span><span class="p">(</span><span class="n">comb_cnt</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">(),</span><span class="w">
              </span><span class="n">alpha_ind</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="nf">sum</span><span class="p">(</span><span class="n">alpha_ind</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">0</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">freq</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">comb_cnt</span><span class="w"> </span><span class="o">/</span><span class="nf">sum</span><span class="p">(</span><span class="n">comb_cnt</span><span class="p">),</span><span class="w">
           </span><span class="n">y_cnt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">comb_cnt</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">ungroup</span><span class="p">()</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">mm_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">plot_set</span><span class="p">,</span><span class="w"> </span><span class="n">x_q</span><span class="p">,</span><span class="w"> </span><span class="n">y_q</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">plot_set</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x_cat</span><span class="p">,</span><span class="w"> </span><span class="n">freq</span><span class="p">,</span><span class="w"> </span><span class="n">width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y_cnt</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y_cat</span><span class="p">,</span><span class="w"> </span><span class="n">alpha</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">alpha_ind</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">geom_bar</span><span class="p">(</span><span class="n">stat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"identity"</span><span class="p">,</span><span class="w"> </span><span class="n">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fill"</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">facet_grid</span><span class="p">(</span><span class="o">~</span><span class="n">x_cat</span><span class="p">,</span><span class="w"> </span><span class="n">scales</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"free_x"</span><span class="p">,</span><span class="w"> </span><span class="n">space</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"free_x"</span><span class="p">,</span><span class="w">
               </span><span class="n">switch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"x"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">theme</span><span class="p">(</span><span class="w">
      </span><span class="n">axis.text.x</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
      </span><span class="n">axis.ticks.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
      </span><span class="n">panel.spacing</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">unit</span><span class="p">(</span><span class="m">0.1</span><span class="p">,</span><span class="w"> </span><span class="s2">"lines"</span><span class="p">),</span><span class="w">
      </span><span class="n">panel.grid.major</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
      </span><span class="n">panel.grid.minor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
      </span><span class="n">panel.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
      </span><span class="n">strip.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">()</span><span class="w">
    </span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">guides</span><span class="p">(</span><span class="n">alpha</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">labs</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">quo_name</span><span class="p">(</span><span class="n">y_q</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
    </span><span class="n">xlab</span><span class="p">(</span><span class="n">quo_name</span><span class="p">(</span><span class="n">x_q</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">set_alpha</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w"> </span><span class="n">plot_return</span><span class="p">,</span><span class="w"> </span><span class="n">a_q</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">mutate</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="w"> </span><span class="o">!!</span><span class="n">a_q</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">pull</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">
      </span><span class="n">unique</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">length</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">`==`</span><span class="p">(</span><span class="m">1</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">plot_return</span><span class="w"> </span><span class="o">+</span><span class="w">
      </span><span class="n">scale_alpha_continuous</span><span class="p">(</span><span class="n">range</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">))</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">plot_return</span><span class="w"> </span><span class="o">+</span><span class="w">
      </span><span class="n">scale_alpha_continuous</span><span class="p">(</span><span class="n">range</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">.4</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">))</span><span class="w">
  </span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">make_text_vec</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">plot_set</span><span class="p">,</span><span class="w"> </span><span class="n">add_text</span><span class="p">,</span><span class="w"> </span><span class="n">round_text</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">add_text</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"n"</span><span class="p">)</span><span class="w"> </span><span class="nf">return</span><span class="p">(</span><span class="n">get_counts</span><span class="p">(</span><span class="n">plot_set</span><span class="p">))</span><span class="w">
  </span><span class="n">text_col</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">get_props</span><span class="p">(</span><span class="n">plot_set</span><span class="p">)</span><span class="w">
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">add_text</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"perc"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">text_col</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">round</span><span class="p">(</span><span class="n">text_col</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">100</span><span class="p">,</span><span class="w"> </span><span class="n">round_text</span><span class="p">)</span><span class="w">
    </span><span class="nf">return</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="n">text_col</span><span class="p">,</span><span class="w"> </span><span class="s2">"%"</span><span class="p">))</span><span class="w">
  </span><span class="p">}</span><span class="w">
  </span><span class="nf">round</span><span class="p">(</span><span class="n">text_col</span><span class="p">,</span><span class="w"> </span><span class="n">round_text</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">get_counts</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">plot_set</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">plot_set</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">pull</span><span class="p">(</span><span class="n">comb_cnt</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">get_props</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">plot_set</span><span class="p">){</span><span class="w">
  </span><span class="n">plot_set</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">text_col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">comb_cnt</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">plot_set</span><span class="o">$</span><span class="n">comb_cnt</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">pull</span><span class="p">()</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">calculate_coordinates</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">plot_return</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">ggplot_build</span><span class="p">(</span><span class="n">plot_return</span><span class="p">)</span><span class="o">$</span><span class="n">data</span><span class="p">[[</span><span class="m">1</span><span class="p">]]</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">split</span><span class="p">(</span><span class="n">.</span><span class="o">$</span><span class="n">PANEL</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">map</span><span class="p">(</span><span class="n">y_in_the_middle</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">unlist</span><span class="p">()</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">y_in_the_middle</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">y_pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="o">$</span><span class="n">y</span><span class="p">)</span><span class="w">
  </span><span class="n">rev</span><span class="p">(</span><span class="n">y_pos</span><span class="p">[</span><span class="o">-</span><span class="nf">length</span><span class="p">(</span><span class="n">y_pos</span><span class="p">)]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="n">y_pos</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">diff</span><span class="p">())</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

To leave a comment for the author, please follow the link and comment on their blog: That’s so Random.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)