{"id":959,"date":"2011-06-29T12:27:10","date_gmt":"2011-06-29T16:27:10","guid":{"rendered":"http:\/\/lukemiller.org\/?p=959"},"modified":"2011-06-29T12:27:10","modified_gmt":"2011-06-29T16:27:10","slug":"a-simple-ggplot2-scatterplot","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2011\/06\/a-simple-ggplot2-scatterplot\/","title":{"rendered":"A simple ggplot2 scatterplot"},"content":{"rendered":"<p>Here&#8217;s a bit of code used to produce one of the figures in my <a href=\"http:\/\/www.biolbull.org\/cgi\/content\/abstract\/220\/3\/209\" target=\"_blank\">recent paper<\/a> dealing with modeling rocky intertidal snail body temperatures. This was my first foray into <a href=\"http:\/\/had.co.nz\/ggplot2\/\" target=\"_blank\">ggplot2<\/a>, and it only involved a few hours of head-scratching. The plot is a comparison of 10 years of\u00a0 daily maximum body temperatures of a modeled littorine snail sitting on a rock with its foot out on the rock or withdrawn into the shell. The original data are here: <a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/dailymax_runs139_169.csv\">dailymax_runs139_169<\/a>.<\/p>\n<p>The code below will open the data file, create the ggplot2 figure, and save the output to a .png file like that shown here. With ~3650 data points plotted, this sort of figure is right on the cusp where you should consider submitting it as a high-resolution tiff or png file rather than as a vector graphic (pdf or svg), since plotting all of those individual points in a vector graphic format can take quite a while. <\/p>\n<figure id=\"attachment_964\" aria-describedby=\"caption-attachment-964\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/figure_x.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/figure_x.png\" alt=\"\" title=\"figure_x\" width=\"600\" height=\"600\" class=\"size-full wp-image-964\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/figure_x.png 600w, https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/figure_x-150x150.png 150w, https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/figure_x-300x300.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-964\" class=\"wp-caption-text\">The figure produced by the code below.<\/figcaption><\/figure>\n<div style=\"overflow:auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family:monospace;\"><a href=\"http:\/\/inside-r.org\/r-doc\/base\/library\"><span style=\"color: #003399; font-weight: bold;\">library<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/ggplot2\"><span style=\"\">ggplot2<\/span><\/a><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">#load ggplot2 package<\/span>\r\n&nbsp;\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a> = <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.csv\"><span style=\"color: #003399; font-weight: bold;\">read.csv<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file.choose\"><span style=\"color: #003399; font-weight: bold;\">file.choose<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">#Load the data into a data frame<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/colnames\"><span style=\"color: #003399; font-weight: bold;\">colnames<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a><span style=\"color: #009900;\">&#41;<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;Foot.in&quot;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">&quot;Foot.out&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;Matlab.Day&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">#rename columns<\/span>\r\n&nbsp;\r\nprevious.theme = theme_set<span style=\"color: #009900;\">&#40;<\/span>theme_bw<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">#set black and white ggplot theme<\/span>\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\">#Define data to be plotted<\/span>\r\ndfplot = <a href=\"http:\/\/inside-r.org\/packages\/cran\/ggplot\"><span style=\"\">ggplot<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a><span style=\"color: #339933;\">,<\/span> aes<span style=\"color: #009900;\">&#40;<\/span>x = Foot.in<span style=\"color: #339933;\">,<\/span> y = Foot.out<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\">#Assemble a x-axis title. The 'atop' function allows you to have two lines of <\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#text in a pasted-together expression<\/span>\r\nmy.xlab = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/expression\"><span style=\"color: #003399; font-weight: bold;\">expression<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/grDevices\/atop\"><span style=\"color: #003399; font-weight: bold;\">atop<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;Maximum Daily Body Temperature, &quot;<\/span><span style=\"color: #339933;\">,<\/span> degree<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;C&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\t\t<span style=\"color: #0000ff;\">&quot;Foot Withdrawn&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#Assemble a y-axis title using the same method. Two spaces are needed after <\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#&quot;Temperature&quot; to make proper spacing on the y-axis title for some reason<\/span>\r\nmy.ylab = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/expression\"><span style=\"color: #003399; font-weight: bold;\">expression<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/grDevices\/atop\"><span style=\"color: #003399; font-weight: bold;\">atop<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;Maximum Daily Body Temperature,  &quot;<\/span><span style=\"color: #339933;\">,<\/span> degree<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;C&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\t\t<span style=\"color: #0000ff;\">&quot;Foot Extended&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n&nbsp;\r\n<span style=\"color: #666666; font-style: italic;\">#Open a new png device to print the figure out to (or use tiff, pdf, etc).<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/grDevices\/png\"><span style=\"color: #003399; font-weight: bold;\">png<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>filename = <span style=\"color: #0000ff;\">&quot;figure_x.png&quot;<\/span><span style=\"color: #339933;\">,<\/span> width = <span style=\"color: #cc66cc;\">600<\/span><span style=\"color: #339933;\">,<\/span> height = <span style=\"color: #cc66cc;\">600<\/span><span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/units\"><span style=\"color: #003399; font-weight: bold;\">units<\/span><\/a> = <span style=\"color: #0000ff;\">'px'<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/print\"><span style=\"color: #003399; font-weight: bold;\">print<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span>dfplot <span style=\"\">+<\/span> \r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Define point shape and set alpha transparency<\/span>\r\n\t\t\t\tgeom_point<span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/shape\"><span style=\"\">shape<\/span><\/a> = <span style=\"color: #cc66cc;\">20<\/span><span style=\"color: #339933;\">,<\/span> color = alpha<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;black&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"\">\/<\/span><span style=\"color: #cc66cc;\">5<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#draw a dashed line of unity with geom_abline<\/span>\r\n\t\t\t\tgeom_abline<span style=\"color: #009900;\">&#40;<\/span>intercept = <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">,<\/span> slope = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span> linetype = <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\txlab<span style=\"color: #009900;\">&#40;<\/span>my.xlab<span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>  <span style=\"color: #666666; font-style: italic;\">#insert the x-axis title<\/span>\r\n\t\t\t\tylab<span style=\"color: #009900;\">&#40;<\/span>my.ylab<span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>  <span style=\"color: #666666; font-style: italic;\">#insert the y-axis title<\/span>\r\n\t\t\t\txlim<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #cc66cc;\">5<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">40<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>  <span style=\"color: #666666; font-style: italic;\">#set the x-axis limits explicitly<\/span>\r\n\t\t\t\tylim<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #cc66cc;\">5<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">40<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>  <span style=\"color: #666666; font-style: italic;\">#set the y-axis limits explicitly<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Adjust the plot margins slightly<\/span>\r\n\t\t\t\topts<span style=\"color: #009900;\">&#40;<\/span>plot.margin = <a href=\"http:\/\/inside-r.org\/r-doc\/grid\/unit\"><span style=\"color: #003399; font-weight: bold;\">unit<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">&quot;lines&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Adjust the font size and margin location of the x-axis title<\/span>\r\n\t\t\t\topts<span style=\"color: #009900;\">&#40;<\/span>axis.title.x = theme_text<span style=\"color: #009900;\">&#40;<\/span>size = <span style=\"color: #cc66cc;\">16<\/span><span style=\"color: #339933;\">,<\/span> vjust = <span style=\"\">-<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Adjust the font, angle, and margin location of the y-axis title<\/span>\r\n\t\t\t\topts<span style=\"color: #009900;\">&#40;<\/span>axis.title.y = theme_text<span style=\"color: #009900;\">&#40;<\/span>size = <span style=\"color: #cc66cc;\">16<\/span><span style=\"color: #339933;\">,<\/span> angle = <span style=\"color: #cc66cc;\">90<\/span><span style=\"color: #339933;\">,<\/span> vjust = <span style=\"color: #cc66cc;\">0.25<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Adjust the font size of the tick labels on the x-axis<\/span>\r\n\t\t\t\topts<span style=\"color: #009900;\">&#40;<\/span>axis.text.x = theme_text<span style=\"color: #009900;\">&#40;<\/span>size = <span style=\"color: #cc66cc;\">14<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Adjust the font size of the tick labels on the y-axis<\/span>\r\n\t\t\t\topts<span style=\"color: #009900;\">&#40;<\/span>axis.text.y = theme_text<span style=\"color: #009900;\">&#40;<\/span>size = <span style=\"color: #cc66cc;\">14<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"\">+<\/span>\r\n\t\t\t\t<span style=\"color: #666666; font-style: italic;\">#Draw only the major grid lines<\/span>\r\n\t\t\t\topts<span style=\"color: #009900;\">&#40;<\/span>panel.grid.minor = theme_blank<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\r\n<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">#end of print statement<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/grDevices\/dev.off\"><span style=\"color: #003399; font-weight: bold;\">dev.off<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">#close the png device to save the figure. <\/span><\/pre>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/www.inside-r.org\/pretty-r\" title=\"Created by Pretty R at inside-R.org\">Created by Pretty R at inside-R.org<\/a><\/p>\n<p>Though the two scatterplots from the paper were generated in this manner, I put together the final 2-panel figure with illustrated snails on it using Adobe Illustrator. It could have all been accomplished in R, but by that point I had already spent enough time wrestling with ggplot2. The actual paper can be found at the <a href=\"http:\/\/www.biolbull.org\/cgi\/content\/abstract\/220\/3\/209\">Biological Bulletin.<\/a><br \/>\n<figure id=\"attachment_966\" aria-describedby=\"caption-attachment-966\" style=\"width: 131px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/final_figure.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/final_figure-131x300.png\" alt=\"\" title=\"final_figure\" width=\"131\" height=\"300\" class=\"size-medium wp-image-966\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/final_figure-131x300.png 131w, https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/06\/final_figure.png 405w\" sizes=\"auto, (max-width: 131px) 100vw, 131px\" \/><\/a><figcaption id=\"caption-attachment-966\" class=\"wp-caption-text\">The final figure assembled from the ggplot2 scatterplots, after some tweaking in Adobe Illustrator.<\/figcaption><\/figure><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a bit of code used to produce one of the figures in my recent paper dealing with modeling rocky intertidal snail body temperatures. This was my first foray into ggplot2, and it only involved a few hours of head-scratching. The plot is a comparison of 10 years of\u00a0 daily maximum body temperatures of a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[82,95,92,91,96,97,93,94],"class_list":["post-959","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-axis-label","tag-body-temperature","tag-expression","tag-ggplot2","tag-littorina","tag-littorines","tag-scatterplot","tag-transparency"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=959"}],"version-history":[{"count":14,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/959\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/959\/revisions\/976"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}