{"id":175266,"date":"2023-12-17T10:05:29","date_gmt":"2023-12-17T09:05:29","guid":{"rendered":"https:\/\/liora.io\/en\/?p=175266"},"modified":"2026-08-09T20:33:00","modified_gmt":"2026-08-09T19:33:00","slug":"ggplot-everything-you-need-to-know-about-the-r-data-visualization-library","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/ggplot-everything-you-need-to-know-about-the-r-data-visualization-library","title":{"rendered":"ggplot : Everything you need to know about the R data visualization library"},"content":{"rendered":"\n<p><strong>In this article, we will explore the fundamental concepts of ggplot and learn how to create a chart using this library to effectively present your data.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-ggplot\">What is ggplot?<\/h2>\n\n\n\n<p><strong>ggplot<\/strong> is a<a href=\"https:\/\/liora.io\/en\/google-data-studio-introduction-to-googles-dataviz-tool\"> data visualization library<\/a> in R, developed by Hadley Wickham in 2005. This library is based on the grammar of graphics, which allows describing graphs in terms of basic components such as axes, legends, or labels.<\/p>\n\n\n\n<p>With ggplot, you can think of a graph as a series of layers that stack up to produce the final graph. Each graph layer can be added using the + function and can include elements such as points, lines, bars, scatterplots, histograms, boxplots, text, and much more.<\/p>\n\n\n\n<p>To create a graph using the layering system in ggplot, start by specifying the data and the variables to use for the x and y axes, then gradually add additional graph layers.<\/p>\n\n\n\n<p>One of the most commonly used graph layers involves geometric functions using the appropriate geom_ functions.<\/p>\n\n\n\n<p>Here are some examples of geometric function graph layers:<\/p>\n\n\n\n<p>&#45; geom_point(): adds points to the graph<br\/>&#45; geom_line(): adds a line to the graph<br\/>&#45; geom_bar(): adds a bar chart to the graph<br\/>&#45; geom_histogram(): adds a histogram to the graph<br\/>&#45; geom_boxplot(): adds a boxplot to the graph<br\/>&#45; geom_text(): adds text to the graph<\/p>\n\n\n\n<p>Please note that each graph layer can be <strong>customized using function-specific options.<\/strong><\/p>\n\n\n\n<p>To understand this principle, let&#8217;s take a step-by-step look at how to create the following chart with the Iris dataset.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for What is ggplot?\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image6.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>Here is an overview of our data:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:370px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"Illustration for What is ggplot?\" decoding=\"async\" height=\"236\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image4-1.png\" style=\"width:370px;max-width:100%;height:auto\" width=\"370\"\/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"\/en\/courses\/data-ai\/data-analyst\">Learn to use ggplot<\/a><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-1-load-the-ggplot-library-and-read-the-csv-file\">Step 1: Load the ggplot library and read the csv file <\/h3>\n\n\n\n<p>library(ggplot2)<\/p>\n\n\n\n<p>iris &lt;- read.csv(&#8220;species.csv&#8221;)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-2-create-the-ggplot-object\">Step 2: Create the ggplot object<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for Step 2: Create the ggplot object\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image5.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>This <strong>line creates a ggplot<\/strong> object named p, which represents the iris dataset with the variables Sepal.Length, Petal.Length, Sepal.Width and Petal.Width. The values of Sepal.Length and Petal.Length are added to create the x-axis, while the values of Sepal.Width and Petal.Width are added to create the y-axis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-3-creating-a-point-cloud\">Step 3: Creating a point cloud<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))\u00a0<\/code><\/pre>\n\n\n\n<p>+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for Step 3: Creating a point cloud\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image3-1.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>This line adds a point cloud (geom_jitter) to the graph. The points are colored according to the Species variable and have a transparency of 0.6 and a width of 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-4-create-a-linear-regression\">Step 4: Create a linear regression<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))\u00a0\n\n+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)\n\n+ geom_smooth(method='lm', se = FALSE)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for Step 4: Create a linear regression\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image7.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>This<a href=\"https:\/\/liora.io\/en\/classification-algorithms-definition-and-main-models\"> line adds a regression line layer<\/a> (geom_smooth) to the graph. The modeling method used is linear regression (method=&#8217;lm&#8217;). The se = FALSE option is used to avoid displaying confidence intervals.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"\/en\/courses\/data-ai\/data-analyst\">Learn to master ggplot with Liora<\/a><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-5-separate-the-graph-into-sub-sections\">Step 5: Separate the graph into sub-sections<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))\n\n+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)\n\n+ geom_smooth(method='lm', se = FALSE)\u00a0\n\n+ facet_wrap(~Species)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for Step 5: Separate the graph into sub-sections\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image2-1.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>This line divides the graph into panels (facet_wrap) according to the Species variable. This allows you to see the relationship between variables for each species separately.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-6-add-labels\">Step 6: Add labels<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width)) + geom_jitter(aes(color = Species), alpha =0.6, width = 1)\n\n+ geom_smooth(method='lm', se = FALSE)\u00a0\n\n+ facet_wrap(~Species)\u00a0\n\n+ labs(title = \"Relation Length\/Width\", x= \"Length\", y= \"Width\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for Step 6: Add labels\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image1.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>This line adds title and axis labels to the graph. The title is &#8220;Relation Length\/Width&#8221;, the x-axis is labelled &#8220;Length&#8221; and the y-axis is labelled &#8220;Width&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-7-adding-the-theme\">Step 7: Adding the theme<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>p = ggplot(iris, aes(x=Sepal.Length + Petal.Length, y = Sepal.Width + Petal.Width))\u00a0\n\n+ geom_jitter(aes(color = Species), alpha =0.6, width = 1)\n\n+ geom_smooth(method='lm', se = FALSE)\u00a0\n\n+ facet_wrap(~Species)\u00a0\n\n+ labs(title = \"Relation Length\/Width\", x= \"Length\", y= \"Width\")\n\n+\u00a0 theme(plot.background = element_rect(fill = '#E8EAF6', color = \"#08104E\", size = 3))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for Step 7: Adding the theme\" decoding=\"async\" height=\"840\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2023\/04\/image6-1.png\" style=\"width:100%;height:auto\" width=\"840\"\/><\/figure>\n\n\n\n<p>This <strong>line defines a custom theme<\/strong> for the graphic using the theme() function. The argument plot.background is used to define the background of the graphic. The element_rect() function is used to create a rectangle with a fill color of &#8216;#E8EAF6&#8217;, a border color of &#8220;#08104E&#8221; and a thickness of 3 pixels.<\/p>\n\n\n\n<p>This code creates a 7-step ggplot graph showing the relationship between sepal and petal length and width for different iris flower species. Points are colored according to species, and a linear regression is fitted for each species.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-do-i-need-to-know-about-ggplot\">What do I need to know about ggplot?<\/h2>\n\n\n\n<p>ggplot is a library for<a href=\"https:\/\/liora.io\/en\/altair-everything-you-need-to-know-about-this-statistical-visualization-library\"> data visualization<\/a> in R. Thanks to its flexible layering system, we can create complex custom graphics by gradually adding additional graphic components.<\/p>\n\n\n\n<p>If you&#8217;re interested in data visualization, don&#8217;t hesitate to join our Data Analyst training course!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"\/en\/courses\/data-ai\/data-analyst\">Register for our Data Analyst training<\/a><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore the fundamental concepts of ggplot and learn how to create a chart using this library to effectively present your data. What is ggplot? ggplot is a data visualization library in R, developed by Hadley Wickham in 2005. This library is based on the grammar of graphics, which allows describing [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":175273,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-175266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=175266"}],"version-history":[{"count":3,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175266\/revisions"}],"predecessor-version":[{"id":211261,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175266\/revisions\/211261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/175273"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=175266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=175266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}