{"id":15897,"date":"2020-04-27T14:20:32","date_gmt":"2020-04-27T13:20:32","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=15897"},"modified":"2020-04-27T14:39:33","modified_gmt":"2020-04-27T13:39:33","slug":"highchart-interactive-scatter-plot-in-r","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/","title":{"rendered":"Highchart Interactive Scatter Plot in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>This article describes how to create an <strong>interactive scatter plot in R<\/strong> using the <em>highchart<\/em> 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-scatter-plots\">Basic scatter plots<\/a><\/li>\n<li><a href=\"#scatter-plots-with-multiple-groups\">Scatter plots with multiple groups<\/a><\/li>\n<li><a href=\"#add-regression-lines\">Add regression lines<\/a><\/li>\n<li><a href=\"#bubble-chart\">Bubble chart<\/a><\/li>\n<li><a href=\"#color-by-a-continuous-variable\">Color by a continuous variable<\/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>library(highcharter) <\/code><\/pre>\n<\/div>\n<div id=\"data-preparation\" class=\"section level2\">\n<h2>Data preparation<\/h2>\n<p>Demo dataset: <code>mtcars<\/code>. The variable <code>cyl<\/code> is used as grouping variable.<\/p>\n<pre class=\"r\"><code># Load data\r\ndata(\"mtcars\")\r\ndf &lt;- mtcars\r\n# Convert cyl as a grouping variable\r\ndf$cyl &lt;- as.factor(df$cyl)\r\n# Inspect the data\r\nhead(df[, c(\"wt\", \"mpg\", \"cyl\", \"qsec\")], 4)<\/code><\/pre>\n<pre><code>##                  wt  mpg cyl qsec\r\n## Mazda RX4      2.62 21.0   6 16.5\r\n## Mazda RX4 Wag  2.88 21.0   6 17.0\r\n## Datsun 710     2.32 22.8   4 18.6\r\n## Hornet 4 Drive 3.21 21.4   6 19.4<\/code><\/pre>\n<\/div>\n<div id=\"basic-scatter-plots\" class=\"section level2\">\n<h2>Basic scatter plots<\/h2>\n<pre class=\"r\"><code>hc &lt;- df %&gt;% hchart('scatter', hcaes(x = wt, y = mpg))<\/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\/004-highcharter-scatter-plot-basic-scatter-plots.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<div id=\"scatter-plots-with-multiple-groups\" class=\"section level2\">\n<h2>Scatter plots with multiple groups<\/h2>\n<pre class=\"r\"><code># Change color by groups\r\n# Set custom colors\r\nhc &lt;- df %&gt;% \r\n  hchart('scatter', hcaes(x = wt, y = mpg, group = cyl)) %&gt;%\r\n  hc_colors(c(\"#00AFBB\", \"#E7B800\", \"#FC4E07\"))<\/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\/004-highcharter-scatter-plot-scatter-plot-with-multiple-groups.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<div id=\"add-regression-lines\" class=\"section level2\">\n<h2>Add regression lines<\/h2>\n<pre class=\"r\"><code># Fit regression model\r\nlibrary(dplyr)\r\nlibrary(broom)\r\nmodel &lt;- lm(mpg ~ wt, data = df)\r\nfit &lt;- augment(model) %&gt;% arrange(wt)\r\n\r\n# Visualization\r\nhc &lt;- df %&gt;% \r\n  hchart('scatter', hcaes(x = wt, y = mpg, group = cyl)) %&gt;%\r\n  hc_add_series(\r\n    fit, type = \"line\", hcaes(x = wt, y = .fitted),\r\n    name = \"Fit\", id = \"fit\"\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\/004-highcharter-scatter-plot-add-regression-lines.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<div id=\"bubble-chart\" class=\"section level2\">\n<h2>Bubble chart<\/h2>\n<p>In a bubble chart, points <code>size<\/code> is controlled by a continuous variable, here <code>qsec<\/code>.<\/p>\n<pre class=\"r\"><code>hc &lt;- df %&gt;% \r\n  hchart(\r\n    'scatter', hcaes(x = wt, y = mpg, size = qsec, group = cyl),\r\n    maxSize = \"10%\"\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\/004-highcharter-scatter-plot-bubble-chart.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<div id=\"color-by-a-continuous-variable\" class=\"section level2\">\n<h2>Color by a continuous variable<\/h2>\n<pre class=\"r\"><code>hc &lt;- df %&gt;% \r\n  hchart('scatter', hcaes(x = wt, y = mpg, color = mpg))<\/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\/004-highcharter-scatter-plot-color-by-continuous-variable.html\" frameborder=\"2\"><br \/>\n<\/iframe><\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Describes how to create an interactive scatter plot in R using the highchart R package.<\/p>\n","protected":false},"author":1,"featured_media":9197,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-15897","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 Scatter 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-scatter-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 Scatter Plot in R: The Essentials - Datanovia\" \/>\n<meta property=\"og:description\" content=\"Describes how to create an interactive scatter plot in R using the highchart R package.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-27T13:39:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.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=\"1 minute\" \/>\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-scatter-plot-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/\",\"name\":\"Highchart Interactive Scatter Plot in R: The Essentials - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg\",\"datePublished\":\"2020-04-27T13:20:32+00:00\",\"dateModified\":\"2020-04-27T13:39:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-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 Scatter 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 Scatter 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-scatter-plot-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Highchart Interactive Scatter Plot in R: The Essentials - Datanovia","og_description":"Describes how to create an interactive scatter plot in R using the highchart R package.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/","og_site_name":"Datanovia","article_modified_time":"2020-04-27T13:39:33+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/","name":"Highchart Interactive Scatter Plot in R: The Essentials - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg","datePublished":"2020-04-27T13:20:32+00:00","dateModified":"2020-04-27T13:39:33+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-plot-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1030975.JPG.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/highchart-interactive-scatter-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 Scatter 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\/15897","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=15897"}],"version-history":[{"count":3,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/15897\/revisions"}],"predecessor-version":[{"id":15900,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/15897\/revisions\/15900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/9197"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=15897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}