{"id":8407,"date":"2019-01-12T10:36:03","date_gmt":"2019-01-12T08:36:03","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?p=8407"},"modified":"2019-12-25T10:59:05","modified_gmt":"2019-12-25T08:59:05","slug":"how-to-save-a-ggplot","status":"publish","type":"post","link":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/","title":{"rendered":"How to Save a GGPlot"},"content":{"rendered":"<div id=\"rdoc\">\n<p>In this article, you will learn how to <strong>save a ggplot<\/strong> to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.<\/p>\n<p>You can either print directly a ggplot into PNG\/PDF files or use the convenient function <code>ggsave()<\/code> for saving a ggplot.<\/p>\n<p>The default of <code>ggsave()<\/code> is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.<\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#basics\">Basics<\/a><\/li>\n<li><a href=\"#save-ggplot-into-a-pdf-file\">Save ggplot into a PDF file<\/a><\/li>\n<li><a href=\"#print-into-a-png-file\">Print into a PNG file<\/a><\/li>\n<li><a href=\"#ggave\">ggave<\/a><\/li>\n<\/ul>\n<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div class='dt-sc-ico-content type1'><div class='custom-icon' ><a href='https:\/\/www.datanovia.com\/en\/product\/ggplot2-essentials-for-great-data-visualization-in-r\/' target='_blank'><span class='fa fa-book'><\/span><\/a><\/div><h4><a href='https:\/\/www.datanovia.com\/en\/product\/ggplot2-essentials-for-great-data-visualization-in-r\/' target='_blank'> Related Book <\/a><\/h4>GGPlot2 Essentials for Great Data Visualization in R<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div id=\"basics\" class=\"section level2\">\n<h2>Basics<\/h2>\n<p>The standard procedure to save any graphics from R is as follow:<\/p>\n<ol style=\"list-style-type: decimal;\">\n<li><strong>Open a graphic device<\/strong> using one of the following functions:<\/li>\n<\/ol>\n<ul>\n<li>pdf(\u201cr-graphics.pdf\u201d),<\/li>\n<li>svg(\u201cr-graphics.svg\u201d),<\/li>\n<li>png(\u201cr-graphics.png\u201d),<\/li>\n<li>tiff(\u201cr-graphics.tiff\u201d),<\/li>\n<li>jpeg(\u201cr-graphics.jpg\u201d),<\/li>\n<li>and so on.<\/li>\n<\/ul>\n<p>Additional arguments indicating the width and the height (in inches) of the graphics region can be also specified in the mentioned function.<\/p>\n<ol style=\"list-style-type: decimal;\" start=\"2\">\n<li><strong>Create and print a plot<\/strong><\/li>\n<li><strong>Close the graphic device<\/strong> using the function <code>dev.off()<\/code><\/li>\n<\/ol>\n<\/div>\n<div id=\"save-ggplot-into-a-pdf-file\" class=\"section level2\">\n<h2>Save ggplot into a PDF file<\/h2>\n<p>For example, to export ggplot2 graphs to a pdf file, the R code looks like this:<\/p>\n<pre class=\"r\"><code># Create some plots\r\nlibrary(ggplot2)\r\nmyplot1 &lt;- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + \r\n  geom_point()\r\nmyplot2 &lt;- ggplot(iris, aes(Species, Sepal.Length)) + \r\n  geom_boxplot()\r\n\r\n# Print plots to a pdf file\r\npdf(\"ggplot.pdf\")\r\nprint(myplot1)     # Plot 1 --&gt; in the first page of PDF\r\nprint(myplot2)     # Plot 2 ---&gt; in the second page of the PDF\r\ndev.off() <\/code><\/pre>\n<\/div>\n<div id=\"print-into-a-png-file\" class=\"section level2\">\n<h2>Print into a PNG file<\/h2>\n<p>For printing to a <strong>png<\/strong> file, use:<\/p>\n<pre class=\"r\"><code>png(\"myplot.png\")\r\nprint(myplot)\r\ndev.off()<\/code><\/pre>\n<\/div>\n<div id=\"ggave\" class=\"section level2\">\n<h2>ggave<\/h2>\n<p>It\u2019s also possible to make a ggplot and to save it from the screen using the function <strong>ggsave()<\/strong>:<\/p>\n<pre class=\"r\"><code># 1. Create a plot: displayed on the screen (by default)\r\nggplot(mtcars, aes(wt, mpg)) + geom_point()\r\n# 2.1. Save the plot to a pdf\r\nggsave(\"myplot.pdf\")\r\n# 2.2 OR save it to png file\r\nggsave(\"myplot.png\")<\/code><\/pre>\n<p>Specify the name of the plot to export:<\/p>\n<pre class=\"r\"><code>p1 &lt;- ggplot(mtcars, aes(wt, mpg)) + geom_point()\r\nggsave(\"myplot.png\", plot = p1)<\/code><\/pre>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc. You can either print directly a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rating_form_position":"","rating_results_position":"","mr_structured_data_type":"","footnotes":""},"categories":[124],"tags":[131],"class_list":["post-8407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ggplot2","tag-ggplot2-faq"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Save a GGPlot: The Best Reference - Datanovia<\/title>\n<meta name=\"description\" content=\"In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.\" \/>\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\/blog\/how-to-save-a-ggplot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Save a GGPlot: The Best Reference - Datanovia\" \/>\n<meta property=\"og:description\" content=\"In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-12T08:36:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-25T08:59:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.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=\"author\" content=\"Alboukadel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alboukadel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/\"},\"author\":{\"name\":\"Alboukadel\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\"},\"headline\":\"How to Save a GGPlot\",\"datePublished\":\"2019-01-12T08:36:03+00:00\",\"dateModified\":\"2019-12-25T08:59:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/\"},\"wordCount\":275,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg\",\"keywords\":[\"ggplot2 FAQ\"],\"articleSection\":[\"ggplot2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/\",\"name\":\"How to Save a GGPlot: The Best Reference - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg\",\"datePublished\":\"2019-01-12T08:36:03+00:00\",\"dateModified\":\"2019-12-25T08:59:05+00:00\",\"description\":\"In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Save a GGPlot\"}]},{\"@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\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\",\"name\":\"Alboukadel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g\",\"caption\":\"Alboukadel\"},\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/author\/kassambara\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Save a GGPlot: The Best Reference - Datanovia","description":"In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.","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\/blog\/how-to-save-a-ggplot\/","og_locale":"en_US","og_type":"article","og_title":"How to Save a GGPlot: The Best Reference - Datanovia","og_description":"In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.","og_url":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/","og_site_name":"Datanovia","article_published_time":"2019-01-12T08:36:03+00:00","article_modified_time":"2019-12-25T08:59:05+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg","type":"image\/jpeg"}],"author":"Alboukadel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Alboukadel","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#article","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/"},"author":{"name":"Alboukadel","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e"},"headline":"How to Save a GGPlot","datePublished":"2019-01-12T08:36:03+00:00","dateModified":"2019-12-25T08:59:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/"},"wordCount":275,"commentCount":0,"publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg","keywords":["ggplot2 FAQ"],"articleSection":["ggplot2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/","url":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/","name":"How to Save a GGPlot: The Best Reference - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg","datePublished":"2019-01-12T08:36:03+00:00","dateModified":"2019-12-25T08:59:05+00:00","description":"In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_9572.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-save-a-ggplot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to Save a GGPlot"}]},{"@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\/"}},{"@type":"Person","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e","name":"Alboukadel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g","caption":"Alboukadel"},"url":"https:\/\/www.datanovia.com\/en\/blog\/author\/kassambara\/"}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/8407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"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=8407"}],"version-history":[{"count":1,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/8407\/revisions"}],"predecessor-version":[{"id":8408,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/8407\/revisions\/8408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/7873"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=8407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/categories?post=8407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/tags?post=8407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}