<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-plot-with-metricsgraphics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/04/13/how-to-plot-with-metricsgraphics/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>Introduction to Metricsgraphics</strong><br>
Metricsgraphics is an htmlwidget interface to the MetricsGraphics.js JavaScript/D3 chart library.</p>
<p><strong>Installation</strong><br>
devtools::install_github(“hrbrmstr/metricsgraphics”)</p>
<p><strong>Usage</strong><br>
Building metricsgraphics charts follows the “piping” idiom, made popular through the magrittr, ggvis and dplyr packages. This makes it possible to avoid one giant function with a ton of parameters and facilitates, breaking out the chart building into logical steps. While MetricsGraphics.js charts may not have the flexibility of the ggplot2, you can build functional, interactive [multi-]lines, scatter-plot, bar charts, histograms and you can even link charts together.</p>
<p>All plots begin with mjs_plot, which sets up the widget. You then use mjs_histograms, mjs_hist, mjs_line, mjs_bar or mjs_point to specify the “geom”” you want to use for plotting. However, unlike the ggplot2 (or even base plot), you cannot combine “geoms.” The only exception to that is adding more lines to a mjs_line plot. This is not a limitation of the package, but more a design principle of the underlying MetricsGraphics JavaScript library.</p>
<p><strong>Examples</strong></p>
<p><strong>Basic Line Chart</strong><br>
This example shows a basic line chart with MetricsGraphics.js baseline [1] &amp; marker [2] annotations:</p>
<p><code>library(htmltools)<br>
library(htmlwidgets)<br>
library(metricsgraphics)<br>
library(RColorBrewer)</code></p>
<p>tmp %<br>
mjs_plot(x=year, y=uspop) %>%<br>
mjs_line() %>%<br>
mjs_add_marker(1850, “Something Wonderful”) %>%<br>
mjs_add_baseline(150, “Something Awful”)</p>
<p><strong>Basic Bar Chart</strong><br>
<code>tmp %>%<br>
mjs_plot(x=uspop, y=year, width=500, height=400) %>%<br>
mjs_bar() %>%<br>
mjs_axis_x(xax_format = 'plain')</code></p>
<p><strong>Scatter-plots</strong><br>
<code>mtcars %>%<br>
mjs_plot(x=wt, y=mpg, width=600, height=500) %>%<br>
mjs_point(color_accessor=carb, size_accessor=carb) %>%<br>
mjs_labs(x=&quot;Weight of Car&quot;, y=&quot;Miles per Gallon&quot;)</code></p>
<p>mtcars %>%<br>
mjs_plot(x=wt, y=mpg, width=600, height=500) %>%<br>
mjs_point(color_accessor=cyl,<br>
x_rug=TRUE, y_rug=TRUE,<br>
size_accessor=carb,<br>
size_range=c(5, 10),<br>
color_type=”category”,<br>
color_range=brewer.pal(n=11, name=”RdBu”)[c(1, 5, 11)]) %>%<br>
mjs_labs(x=”Weight of Car”, y=”Miles per Gallon”) %>%<br>
mjs_add_legend(legend=”X”)</p>
<p>mtcars %>%<br>
mjs_plot(x=wt, y=mpg, width=600, height=500) %>%<br>
mjs_point(least_squares=TRUE) %>%<br>
mjs_labs(x=”Weight of Car”, y=”Miles per Gallon”)</p>
<p><strong>Muti-line Charts</strong><br>
<code>set.seed(1492)<br>
stocks %<br>
mjs_plot(x=time, y=X) %>%<br>
mjs_line() %>%<br>
mjs_add_line(Y) %>%<br>
mjs_add_line(Z) %>%<br>
mjs_axis_x(xax_format=&quot;date&quot;) %>%<br>
mjs_add_legend(legend=c(&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;))</code></p>
<p><strong>Grid</strong><br>
Mjs_grid is patterned after grid.arrange and lets you place many metricsgraphics plots in a grid.</p>
<p><code>lapply(1:7, function(x) {<br>
mjs_plot(rnorm(10000, mean=x/2, sd=x), width=300, height=300) %>%<br>
mjs_histogram(bar_margin=2) %>%<br>
mjs_labs(x_label=sprintf(&quot;Plot %d&quot;, x))<br>
}) -> plots</code></p>
<p>mjs_grid(plots)</p>
<p>lapply(1:7, function(x)<br>
mjs_plot(rbeta(10000, x, x), width=300, height=300) %>%<br>
mjs_histogram(bar_margin=2) %>%<br>
mjs_labs(x_label=sprintf(“Plot %d”, x))<br>
}) -> moar_plots</p>
<p>mjs_grid(moar_plots, nrow=4, ncol=3, widths=c(rep(0.33, 3)))</p>
<p><strong>Linked Charts</strong><br>
<code>stocks2 &lt;- data.frame(<br>
time = as.Date('2009-01-01') + 0:9,<br>
X = rnorm(10, 0, 1),<br>
Y = rnorm(10, 0, 2),<br>
Z = rnorm(10, 0, 4))</code></p>
<p>s1 %<br>
mjs_plot(x=time, y=X, linked=TRUE, width=350, height=275) %>%<br>
mjs_line() %>%<br>
mjs_add_line(Y) %>%<br>
mjs_add_line(Z) %>%<br>
mjs_axis_x(xax_format=”date”) %>%<br>
mjs_add_legend(legend=c(“X”, “Y”, “Z”))</p>
<p>s2 %<br>
mjs_plot(x=time, y=X, linked=TRUE, width=350, height=275) %>%<br>
mjs_line() %>%<br>
mjs_add_line(Y) %>%<br>
mjs_add_line(Z) %>%<br>
mjs_axis_x(xax_format=”date”) %>%<br>
mjs_add_legend(legend=c(“X”, “Y”, “Z”))</p>
<p>mjs_grid(s1, s2, ncol=2)</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/08/06/data-visualization-with-googlevis-exercises-part-10/" title="Data visualization with googleVis exercises part 10" target="_blank">Data visualization with googleVis exercises part 10 </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/06/25/data-visualization-with-googlevis-exercises-part-4/" title="Data Visualization with googleVis exercises part 4" target="_blank">Data Visualization with googleVis exercises part 4 </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/07/23/data-visualization-with-googlevis-exercises-part-8/" title="Data Visualization with googleVis exercises part 8" target="_blank">Data Visualization with googleVis exercises part 8 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-exercises.com/2018/04/13/how-to-plot-with-metricsgraphics/"> R-exercises</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Zf-qErg84TE" height="1"></a></div>