{"id":78473,"date":"2020-05-04T13:09:49","date_gmt":"2020-05-04T07:39:49","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78473"},"modified":"2020-05-04T13:09:49","modified_gmt":"2020-05-04T07:39:49","slug":"saving-graphs-to-files-in-r","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/","title":{"rendered":"Saving Graphs to Files in R in Easy Steps"},"content":{"rendered":"<p>In the previous tutorials, we have been learning about <a href=\"https:\/\/techvidvan.com\/tutorials\/r-graphical-models\/\">various kinds of graphs<\/a> and plots in R. In this chapter of the <strong>TechVidvan\u2019s R tutorial series,<\/strong> we are going to look at how saving graphs to files in R works. We will also look at how to save the graphs in files manually by using the export option.<\/p>\n<h2><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/Saving-Graphs-in-Files.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78474\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/Saving-Graphs-in-Files.jpg\" alt=\"Saving Graphs to Files in R\" width=\"802\" height=\"420\" \/><\/a><\/h2>\n<p>Saving graphs to files in R<\/p>\n<p>It is quite simple to save graphs as files in R programming. R provides different functions to create different kinds of files. We can use these functions to save graphs in pdf, jpeg, png, and also as bmp files. So let\u2019s take a look at these one-by-one.<\/p>\n<h3>1. Saving graphs as JPEG<\/h3>\n<p>We can create a jpeg file and then save a graph in it by using the<strong> jpeg()<\/strong> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">jpeg(\"barplot.jpeg\")\nbarplot(airquality$Ozone, \n        main=\"Ozone concentration in air\", \n        xlab='ozone levels', \n        col=\"blue\", \n        horiz=TRUE)\ndev.off()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/1.saving-graphs-in-files-in-r-jpeg-function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78475\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/1.saving-graphs-in-files-in-r-jpeg-function.png\" alt=\"Saving JPEG files in R\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<p>Using the jpeg command, we create a jpeg file. R starts storing all output into the file. We use the barplot() function to create the graph. This stores the graph into the jpeg file. Finally, we use the dev.off() command to stop recording and save the file.<\/p>\n<h3>2. Saving graphs as PNG<\/h3>\n<p>The <strong>png()<\/strong> function in R can create and save graphs in a png file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">png(\"barplot.png\")\nbarplot(airquality$Ozone, \n        main=\"Ozone concentration in air\", \n        xlab='ozone levels', \n        col=\"blue\", \n        horiz=TRUE)\ndev.off()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/2.saving-graphs-in-files-in-r-png-function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78476\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/2.saving-graphs-in-files-in-r-png-function.png\" alt=\"Saving PNG file in R\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<h3>3. Saving graphs as PDF<\/h3>\n<p>We can use the <strong>pdf()<\/strong> function to save graphs as a pdf file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">pdf(\"barplot.pdf\")\nbarplot(airquality$Ozone, \n        main=\"Ozone concentration in air\", \n        xlab='ozone levels', \n        col=\"blue\", \n        horiz=TRUE)\ndev.off()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/3.saving-graphs-in-files-in-r-pdf-function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78477\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/3.saving-graphs-in-files-in-r-pdf-function.png\" alt=\"Saving PDF files in R\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<h3>4. Saving graphs to Files in R using the export option<\/h3>\n<p>The plot window in <a href=\"https:\/\/techvidvan.com\/tutorials\/rstudio-tutorial\/\">Rstudio<\/a>\u00a0is where graphical outputs are shown. The plot window has an export option that can be used to store graphs as files. Let\u2019s take a look at this.<\/p>\n<p><strong>Step 1: Create a graph<\/strong><\/p>\n<p>You firstly need to create a graph as shown below:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/4.saving-graphs-in-files-in-r-export-step1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78478\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/4.saving-graphs-in-files-in-r-export-step1.png\" alt=\"Export Image in R\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<p><strong>Step 2: Click on the export -&gt; save as image<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/5.saving-graphs-in-files-in-r-export-step-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78479\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/5.saving-graphs-in-files-in-r-export-step-2.png\" alt=\"Export Image in r\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<p><strong>Step 3: You can then select the format of the output file.<\/strong><\/p>\n<p>Select the required format from the options as shown below:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/6.saving-graphs-in-files-in-r-export-step-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78480\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/6.saving-graphs-in-files-in-r-export-step-3.png\" alt=\"Save files in R\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<p><strong>Step 4: Click on save.<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/7.saving-graphs-in-files-inr-export-step-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78481\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/7.saving-graphs-in-files-inr-export-step-4.png\" alt=\"Export Graphs in Files\" width=\"1366\" height=\"768\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Finally we saw there are multiple ways for Saving graph to files in R. Depending on the required file format, we can use different functions, like the jpeg() function, or the png() function. Finally, we looked at the export option and how it can help us with saving graphs in files of our desired format.<\/p>\n<p>Hope you enjoyed the article. Do follow us on <a href=\"https:\/\/www.linkedin.com\/company\/31037567\/\">LinkedIn<\/a> to get updates on more published articles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous tutorials, we have been learning about various kinds of graphs and plots in R. In this chapter of the TechVidvan\u2019s R tutorial series, we are going to look at how saving&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78474,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[2562,2563,2564,2565],"class_list":["post-78473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-creating-and-saving-graphs-r","tag-how-to-save-graphics-to-an-image-file-in-r","tag-save-graphs-to-files-in-r","tag-saving-graphs-to-files"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Saving Graphs to Files in R in Easy Steps - TechVidvan<\/title>\n<meta name=\"description\" content=\"Saving Graphs to Files in R - Learn how to save various types of Graphs in R - How to save as JPEG and PNG, how to use export option to save file in R\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Saving Graphs to Files in R in Easy Steps - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Saving Graphs to Files in R - Learn how to save various types of Graphs in R - How to save as JPEG and PNG, how to use export option to save file in R\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-04T07:39:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Saving-Graphs-in-Files.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Saving Graphs to Files in R in Easy Steps - TechVidvan","description":"Saving Graphs to Files in R - Learn how to save various types of Graphs in R - How to save as JPEG and PNG, how to use export option to save file in R","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:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Saving Graphs to Files in R in Easy Steps - TechVidvan","og_description":"Saving Graphs to Files in R - Learn how to save various types of Graphs in R - How to save as JPEG and PNG, how to use export option to save file in R","og_url":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-05-04T07:39:49+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Saving-Graphs-in-Files.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Saving Graphs to Files in R in Easy Steps","datePublished":"2020-05-04T07:39:49+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/"},"wordCount":396,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Saving-Graphs-in-Files.jpg","keywords":["Creating and Saving Graphs - R","How to Save Graphics to an Image File in R","Save Graphs to files in R","Saving Graphs to Files"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/","url":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/","name":"Saving Graphs to Files in R in Easy Steps - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Saving-Graphs-in-Files.jpg","datePublished":"2020-05-04T07:39:49+00:00","description":"Saving Graphs to Files in R - Learn how to save various types of Graphs in R - How to save as JPEG and PNG, how to use export option to save file in R","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Saving-Graphs-in-Files.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Saving-Graphs-in-Files.jpg","width":802,"height":420,"caption":"Saving Graphs to Files in R"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/saving-graphs-to-files-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Saving Graphs to Files in R in Easy Steps"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78473"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78474"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}