{"id":358,"date":"2015-07-12T09:14:57","date_gmt":"2015-07-12T08:14:57","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=358"},"modified":"2025-07-01T08:33:53","modified_gmt":"2025-07-01T07:33:53","slug":"plots","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/plots\/","title":{"rendered":"Plots"},"content":{"rendered":"\n<p>Plots can be created with the ggplot package<sup class='sup-ref-note' id='note-zotero-ref-p358-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p358-r1'>1<\/a><\/sup>. The plot can be subsequently saved in the desired format. As described, <a href=\"https:\/\/pcool.dyndns.org\/index.php\/graphics\/\" data-type=\"page\" data-id=\"73\">scalable vector graphics<\/a> are advised, but ultimately the choice of format depends on its subsequent use \/ journal requirements.<\/p>\n\n\n\n<p>Rather that creating the plots and saving them it may be more convenient to save the plot directly in the desired format. For example to create a plot of Anscombe&#8217;s<sup class='sup-ref-note' id='note-zotero-ref-p358-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p358-r2'>2<\/a><\/sup> first <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/anscombe.rda\" target=\"_blank\" rel=\"noreferrer noopener\">data set<\/a>; load the dataset and create a plot with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>ggplot(<span style=\"color: #ff0000;\"><em>data=anscombe.quartet<\/em><\/span>, <span style=\"color: #ff0000;\"><em>aes(x = X1,y = Y1)<\/em><\/span>) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_smooth(method = 'lm') + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(label = 'Anscombe\\'s First Data Set') + xlab(label = 'X1 &#091;cm]') + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ylab(label = 'Y1 &#091;cm]') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw()<\/em><\/span><\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/R_functions\/plots.txt\" target=\"_blank\" rel=\"noreferrer noopener\">The plot can subsequently be saved<\/a> in the appropriate format.<\/p>\n\n\n\n<p>To save directly as a PDF file in the <strong>default directory of R<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>pdf('AnscombePlot.pdf')<\/em><\/span>\n\n<span style=\"color: #ff0000;\"><em><span style=\"color: #ff0000;\"><em>ggplot(<span style=\"color: #ff0000;\"><em>data=anscombe.quartet<\/em><\/span>, <span style=\"color: #ff0000;\"><em>aes(x = X1,y = Y1)<\/em><\/span>) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_smooth(method = 'lm') + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(label = 'Anscombe\\'s First Data Set') + xlab(label = 'X1 &#091;cm]') + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ylab(label = 'Y1 &#091;cm]') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw()<\/em><\/span><\/em><\/span>\n\n<span style=\"color: #ff0000;\"><em>dev.off()<\/em><\/span><\/code><\/pre>\n\n\n\n<p>To create a jpg file:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>jpeg('AnscombePlot.jpg',quality=100,width=960,height=960,res=200)<\/em><\/span>\n\n<span style=\"color: #ff0000;\"><em>ggplot(<span style=\"color: #ff0000;\"><em>data=anscombe.quartet<\/em><\/span>, <span style=\"color: #ff0000;\"><em>aes(x = X1,y = Y1)<\/em><\/span>) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_smooth(method = 'lm') + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(label = 'Anscombe\\'s First Data Set') + xlab(label = 'X1 &#091;cm]') + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ylab(label = 'Y1 &#091;cm]') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw()<\/em><\/span>\n\n<span style=\"color: #ff0000;\"><em>dev.off()<\/em><\/span><\/code><\/pre>\n\n\n\n<p>There are similar commands for different graphical formats. For bitmap:<\/p>\n\n\n\n<p><em><span style=\"color: #ff0000;\">??png<\/span><\/em><\/p>\n\n\n\n<p>and for scalable vector graphics:<\/p>\n\n\n\n<p><em><span style=\"color: #ff0000;\">??svg<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Plots can be created with the ggplot package. The plot can be subsequently saved in the desired format. As described, scalable vector graphics are advised, but ultimately the choice of format depends on its subsequent use \/ journal requirements. Rather that creating the plots and saving them it may be more convenient to save the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-358","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=358"}],"version-history":[{"count":4,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/358\/revisions"}],"predecessor-version":[{"id":4645,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/358\/revisions\/4645"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}