{"id":15906,"date":"2020-04-27T15:44:50","date_gmt":"2020-04-27T14:44:50","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=15906"},"modified":"2020-04-27T16:26:11","modified_gmt":"2020-04-27T15:26:11","slug":"highchart-interactive-bar-plot-in-r","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/","title":{"rendered":"Highchart Interactive Bar Plot in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>You will learn how to create an <strong>interactive Bar Plot in R<\/strong> using the highchart R package.<\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#loading-required-r-packages\">Loading required R packages<\/a><\/li>\n<li><a href=\"#data-preparation\">Data preparation<\/a><\/li>\n<li><a href=\"#basic-barplots\">Basic barplots<\/a><\/li>\n<li><a href=\"#change-barplot-colors-by-groups\">Change barplot colors by groups<\/a><\/li>\n<li><a href=\"#barplot-with-multiple-groups\">Barplot with multiple groups<\/a><\/li>\n<\/ul>\n<\/div>\n<div id=\"loading-required-r-packages\" class=\"section level2\">\n<h2>Loading required R packages<\/h2>\n<pre class=\"r\"><code># Load required R packages\r\nlibrary(highcharter) \r\n# Set highcharter options\r\noptions(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 2)))<\/code><\/pre>\n<\/div>\n<div id=\"data-preparation\" class=\"section level2\">\n<h2>Data preparation<\/h2>\n<p>We\u2019ll create two data frames derived from the <code>ToothGrowth<\/code> datasets.<\/p>\n<pre class=\"r\"><code>df &lt;- data.frame(dose=c(\"D0.5\", \"D1\", \"D2\"),\r\n                len=c(4.2, 10, 29.5))\r\n\r\nhead(df)<\/code><\/pre>\n<pre><code>##   dose  len\r\n## 1 D0.5  4.2\r\n## 2   D1 10.0\r\n## 3   D2 29.5<\/code><\/pre>\n<pre class=\"r\"><code>df2 &lt;- data.frame(supp=rep(c(\"VC\", \"OJ\"), each=3),\r\n                dose=rep(c(\"D0.5\", \"D1\", \"D2\"),2),\r\n                len=c(6.8, 15, 33, 4.2, 10, 29.5))\r\n\r\nhead(df2)<\/code><\/pre>\n<pre><code>##   supp dose  len\r\n## 1   VC D0.5  6.8\r\n## 2   VC   D1 15.0\r\n## 3   VC   D2 33.0\r\n## 4   OJ D0.5  4.2\r\n## 5   OJ   D1 10.0\r\n## 6   OJ   D2 29.5<\/code><\/pre>\n<ul>\n<li><code>len<\/code>: Tooth length<\/li>\n<li><code>dose<\/code>: Dose in milligrams (0.5, 1, 2)<\/li>\n<li><code>supp<\/code>: Supplement type (VC or OJ)<\/li>\n<\/ul>\n<\/div>\n<div id=\"basic-barplots\" class=\"section level2\">\n<h2>Basic barplots<\/h2>\n<p>Basic vertical barplots:<\/p>\n<pre class=\"r\"><code>hc &lt;- df %&gt;%\r\n  hchart('column', hcaes(x = dose, y = len))<\/code><\/pre>\n<pre class=\"r\"><code>hc<\/code><\/pre>\n<p><iframe width=\"95%\" height=\"450\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/highcharter\/figures\/010-highcharter-barplot-basic-barplot.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<p>Make horizontal bar plot:<\/p>\n<pre class=\"r\"><code>hc &lt;- df %&gt;%\r\n  hchart(\r\n  'bar', hcaes(x = dose, y = len),\r\n  color = \"lightgray\", borderColor = \"black\"\r\n  )<\/code><\/pre>\n<pre class=\"r\"><code>hc<\/code><\/pre>\n<p><iframe width=\"95%\" height=\"450\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/highcharter\/figures\/010-highcharter-barplot-horizontal-barplot.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<p>Change the width of bars using the argument <code>pointWidth<\/code> (e.g.: width = 15).<\/p>\n<pre class=\"r\"><code># Change bar widths\r\ndf %&gt;% hchart(\r\n  'column', hcaes(x = dose, y = len), \r\n  pointWidth = 15\r\n  )<\/code><\/pre>\n<\/div>\n<div id=\"change-barplot-colors-by-groups\" class=\"section level2\">\n<h2>Change barplot colors by groups<\/h2>\n<p>We\u2019ll change the barplot line and fill color by the variable <code>dose<\/code> group levels.<\/p>\n<pre class=\"r\"><code># Change barplot fill colors by groups\r\nhc &lt;- df %&gt;% \r\n  hchart(\r\n  'column', hcaes(x = dose, y = len, color = dose)\r\n  )<\/code><\/pre>\n<pre class=\"r\"><code>hc<\/code><\/pre>\n<p><iframe width=\"95%\" height=\"450\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/highcharter\/figures\/010-highcharter-barplot-change-color-by-groups.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<div id=\"barplot-with-multiple-groups\" class=\"section level2\">\n<h2>Barplot with multiple groups<\/h2>\n<p><strong>Create stacked and dodged bar plots<\/strong>. Use the functions <code>hc_colors()<\/code> to set manually the bars colors.<\/p>\n<p>Dodged bars:<\/p>\n<pre class=\"r\"><code>hc &lt;- df2 %&gt;% \r\n  hchart('column', hcaes(x = 'dose', y = 'len', group = 'supp')) %&gt;%\r\n  hc_colors(c(\"#0073C2FF\", \"#EFC000FF\"))<\/code><\/pre>\n<pre class=\"r\"><code>hc<\/code><\/pre>\n<p><iframe width=\"95%\" height=\"450\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/highcharter\/figures\/010-highcharter-barplot-dodged-bars.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<p>Stacked bar plots:<\/p>\n<pre class=\"r\"><code>hc &lt;- df2 %&gt;% \r\n  hchart(\r\n    'column', hcaes(x = 'dose', y = 'len', group = 'supp'),\r\n    stacking = \"normal\"\r\n    ) %&gt;%\r\n  hc_colors(c(\"#0073C2FF\", \"#EFC000FF\"))<\/code><\/pre>\n<pre class=\"r\"><code>hc<\/code><\/pre>\n<p><iframe width=\"95%\" height=\"450\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/highcharter\/figures\/010-highcharter-barplot-stacked-bars.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You will learn how to create an interactive Bar Plot in R using the highchart R package.<\/p>\n","protected":false},"author":1,"featured_media":9122,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-15906","dt_lessons","type-dt_lessons","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Highchart Interactive Bar Plot in R: The Essentials - Datanovia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Highchart Interactive Bar Plot in R: The Essentials - Datanovia\" \/>\n<meta property=\"og:description\" content=\"You will learn how to create an interactive Bar Plot in R using the highchart R package.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-27T15:26:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/\",\"name\":\"Highchart Interactive Bar Plot in R: The Essentials - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg\",\"datePublished\":\"2020-04-27T14:44:50+00:00\",\"dateModified\":\"2020-04-27T15:26:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lessons\",\"item\":\"https:\/\/www.datanovia.com\/en\/lessons\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Highchart Interactive Bar Plot in R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Highchart Interactive Bar Plot in R: The Essentials - Datanovia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Highchart Interactive Bar Plot in R: The Essentials - Datanovia","og_description":"You will learn how to create an interactive Bar Plot in R using the highchart R package.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/","og_site_name":"Datanovia","article_modified_time":"2020-04-27T15:26:11+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/","name":"Highchart Interactive Bar Plot in R: The Essentials - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg","datePublished":"2020-04-27T14:44:50+00:00","dateModified":"2020-04-27T15:26:11+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040400.JPG.jpg.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-bar-plot-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Lessons","item":"https:\/\/www.datanovia.com\/en\/lessons\/"},{"@type":"ListItem","position":3,"name":"Highchart Interactive Bar Plot in R"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/15906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/dt_lessons"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=15906"}],"version-history":[{"count":1,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/15906\/revisions"}],"predecessor-version":[{"id":15907,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/15906\/revisions\/15907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/9122"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=15906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}