{"id":228,"date":"2010-05-04T18:25:21","date_gmt":"2010-05-04T22:25:21","guid":{"rendered":"http:\/\/lukemiller.org\/?p=228"},"modified":"2011-05-10T16:48:52","modified_gmt":"2011-05-10T20:48:52","slug":"modifying-basic-plots-in-r","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2010\/05\/modifying-basic-plots-in-r\/","title":{"rendered":"Modifying basic plots in R"},"content":{"rendered":"<p>Below is a walk-through of some of the basics of customizing plots in R. These are all based on the <code>graphics<\/code> package that comes in the base installation of R.<\/p>\n<p>Let&#8217;s start by making a basic plot in R. In the code snippets below, green text behind a # sign is considered comments by R, so everything after a # sign on a line will be ignored by R. We&#8217;ll call the <code>plot<\/code> command and supply it with two vectors of numbers representing the x and y values:<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1)) <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\n<\/code><br \/>\nwhich produces this plot:<\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The basic plot.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The basic plot isn&#8217;t the most attractive thing in the world. We can start prettying it up by adjusting the graphical parameters of the plot. This is most easily done by inserting parameter definitions into the plot command.<\/p>\n<p>We&#8217;ll start by adding labels to the x and y axes, along with a plot title. Note the commas at the end of each line, they are important. Newly inserted code is highlighted in red:<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\n<span style=\"color: #993300;\">xlab = \"Sample number\",<\/span> <span style=\"color: #008000;\">#X axis title<\/span><br \/>\n<span style=\"color: #993300;\">ylab = \"Sample value\",<\/span> <span style=\"color: #008000;\">#Y axis title<\/span><br \/>\n<span style=\"color: #993300;\">main = \"Test\",<\/span> <span style=\"color: #008000;\"> #Main title of plot<\/span><br \/>\n)\t\t\t <span style=\"color: #008000;\">#Closing parenthesis for plot command<\/span><br \/>\n<\/code><br \/>\nwhich makes the following plot:<\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot2.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">Now with axis labels and a title. <\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>One thing that bugs me about the basic R plot is that the y-axis tick labels are rotated<br \/>\nto a vertical orientation. I don&#8217;t think that it&#8217;s ever advisable to make a plot<br \/>\n<em>harder<\/em> to read, so I like my y-axis labels horizontal. We can add one more line to<br \/>\nthe plot command to make the tick labels horizontal:<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\",  <span style=\"color: #008000;\">#X axis title<\/span><br \/>\nylab = \"Sample value\", <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", \t\t <span style=\"color: #008000;\">#Main title of plot<\/span><br \/>\n<span style=\"color: #993300;\">las = 1,<\/span> <span style=\"color: #008000;\"> #Rotate all tick labels horizontal<\/span><br \/>\n)\t\t\t <span style=\"color: #008000;\">#Closing parenthesis for plot command<br \/>\n<\/span> <\/code><br \/>\nwhich makes this plot:<\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot3.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">Note the y-axis tick labels are now horizontal.<\/figcaption><\/figure>\n<p>The <code>las<\/code> parameter has 4 possible values: 0 = parallel to axis, 1 = all horizontal, 2 =<br \/>\nperpendicular to axis, 3 = all vertical. When we modify the <code>las<\/code> parameter in the plot<br \/>\ncommand, it applies to the tick labels on both the x and y axes at the same time.<br \/>\n<img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nNow we can start going hog-wild and changing other parameters to further modify the plot.<br \/>\nLet&#8217;s make this plot have lines between the points by setting the <code>type<\/code> parameter, and<br \/>\nlet&#8217;s make those lines dotted using the <code>lty<\/code> parameter.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\", <span style=\"color: #008000;\"> #X axis title<\/span><br \/>\nylab = \"Sample value\",   <span style=\"color: #008000;\">#Y axis title<\/span><br \/>\nmain = \"Test\", \t\t <span style=\"color: #008000;\">#Main title of plot<\/span><br \/>\nlas = 1,\t<span style=\"color: #008000;\"> #Rotate all tick labels horizontal<\/span><br \/>\n<span style=\"color: #993300;\">type = \"b\",<\/span> <span style=\"color: #008000;\"> #Plot points with lines between them.<\/span><br \/>\n<span style=\"color: #993300;\">lty = 3,<\/span> <span style=\"color: #008000;\">#Define line type. 0 = blank, 3 = dotted<\/span><br \/>\n)\t\t\t <span style=\"color: #008000;\">#Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot4.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The points are now connected by a dotted line.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nThe font style and size can be modified as well. The following command will use<br \/>\n<code>font.axis<\/code> to change the axis tick labels to bold italics, and <code>font.main<\/code> to change the<br \/>\nmain title to italics. Why would you ever do this? I&#8217;m not sure.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\", <span style=\"color: #008000;\"> #X axis title<\/span><br \/>\nylab = \"Sample value\", <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", \t\t <span style=\"color: #008000;\">#Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\ntype = \"b\",\t<span style=\"color: #008000;\"> #Plot points with lines between them.<\/span><br \/>\nlty = 3,\t\t <span style=\"color: #008000;\">#Define line type. 0 = blank, 3 = dotted<\/span><br \/>\n<span style=\"color: #993300;\">font.axis = 4,<\/span> <span style=\"color: #008000;\">#Display axis tick labels in bold italics<\/span><br \/>\n<span style=\"color: #993300;\">font.main = 3,<\/span> <span style=\"color: #008000;\"> #Display the main title in italics<\/span><br \/>\n)\t<span style=\"color: #008000;\"> #Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot5.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The tick labels are in italics for some reason.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nNext let&#8217;s try making the axis tick labels larger using <code>cex.axis<\/code> and make<br \/>\nthe main title larger using <code>cex.main<\/code>.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\",  <span style=\"color: #008000;\">#X axis title<\/span><br \/>\nylab = \"Sample value\",   <span style=\"color: #008000;\">#Y axis title<\/span><br \/>\nmain = \"Test\", <span style=\"color: #008000;\"> #Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\ntype = \"b\",\t\t <span style=\"color: #008000;\">#Plot points with lines between them.<\/span><br \/>\nlty = 3,\t<span style=\"color: #008000;\"> #Define line type. 0 = blank, 3 = dotted<\/span><br \/>\nfont.axis = 4,\t\t <span style=\"color: #008000;\">#Display axis tick labels in bold italics<\/span><br \/>\nfont.main = 3,\t\t <span style=\"color: #008000;\">#Display the main title in italics<\/span><br \/>\n<span style=\"color: #993300;\">cex.axis = 1.2,<\/span> <span style=\"color: #008000;\">#Make axis tick labels 1.2x larger than regular font<\/span><br \/>\n<span style=\"color: #993300;\">cex.main = 1.5,<\/span> <span style=\"color: #008000;\">#Make main title 1.5x larger than regular font size<\/span><br \/>\n)\t\t\t <span style=\"color: #008000;\">#Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot6.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The labels have gotten larger.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nWe can also make the axis titles larger using the <code>cex.lab<\/code> parameter:<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\", <span style=\"color: #008000;\"> #X axis title<\/span><br \/>\nylab = \"Sample value\", <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", \t\t <span style=\"color: #008000;\">#Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\ntype = \"b\",\t\t <span style=\"color: #008000;\">#Plot points with lines between them.<\/span><br \/>\nlty = 3,\t<span style=\"color: #008000;\"> #Define line type. 0 = blank, 3 = dotted<\/span><br \/>\nfont.axis = 4,\t<span style=\"color: #008000;\"> #Display axis tick labels in bold italics<\/span><br \/>\nfont.main = 3,\t<span style=\"color: #008000;\"> #Display the main title in italics<\/span><br \/>\ncex.axis = 1.2,\t<span style=\"color: #008000;\"> #Make axis tick labels 1.2x larger than regular font<\/span><br \/>\ncex.main = 1.5,\t  \t <span style=\"color: #008000;\">#Make main title 1.5x larger than regular font size<\/span><br \/>\n<span style=\"color: #993300;\">cex.lab = 1.5,<\/span> <span style=\"color: #008000;\"> #Make axis title 1.5x larger than regular font size<\/span><br \/>\n)\t<span style=\"color: #008000;\"> #Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot7.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">Even the axis titles are larger.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nMaybe the journal you&#8217;re submitting to doesn&#8217;t like figures to be enclosed in a box. The<br \/>\naxes that are displayed can be modified with the <code>bty<\/code> parameter. Here we&#8217;ll remove the<br \/>\ntop and right side axes.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\", <span style=\"color: #008000;\"> #X axis title<\/span><br \/>\nylab = \"Sample value\", <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", \t\t <span style=\"color: #008000;\">#Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\ntype = \"b\",\t\t <span style=\"color: #008000;\">#Plot points with lines between them.<\/span><br \/>\nlty = 3,\t<span style=\"color: #008000;\"> #Define line type. 0 = blank, 3 = dotted<\/span><br \/>\nfont.axis = 4,\t<span style=\"color: #008000;\"> #Display axis tick labels in bold italics<\/span><br \/>\nfont.main = 3,\t\t <span style=\"color: #008000;\">#Display the main title in italics<\/span><br \/>\ncex.axis = 1.2,\t<span style=\"color: #008000;\"> #Make axis tick labels 1.2x larger than regular font<\/span><br \/>\ncex.main = 1.5,\t<span style=\"color: #008000;\"> #Make main title 1.5x larger than regular font size<\/span><br \/>\ncex.lab = 1.5,\t\t <span style=\"color: #008000;\">#Make axis title 1.5x larger than regular font size<\/span><br \/>\n<span style=\"color: #993300;\">bty = \"l\", <\/span> <span style=\"color: #008000;\">#Specify which axes to draw (l = left + bottom)<\/span><br \/>\n)\t\t\t <span style=\"color: #008000;\">#Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot8.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The upper and right sides of the plot box are now gone.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nPerhaps you want to squeeze the axes titles in a bit closer to the actual plot. This can<br \/>\nbe accomplished with the <code>mgp<\/code> parameter, which also affects the location of the axis<br \/>\ntick labels and the actual ticks. You supply <code>mgp<\/code> with a 3-value vector, where the 1st<br \/>\nvalue is the location of the axis title, the 2nd value is the tick label location, and<br \/>\nthe 3rd value is the tick location. We&#8217;ll move the axis titles in slightly here.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\",  <span style=\"color: #008000;\">#X axis title<\/span><br \/>\nylab = \"Sample value\", <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", <span style=\"color: #008000;\"> #Main title of plot<\/span><br \/>\nlas = 1,\t<span style=\"color: #008000;\"> #Rotate all tick labels horizontal<\/span><br \/>\ntype = \"b\",\t<span style=\"color: #008000;\"> #Plot points with lines between them.<\/span><br \/>\nlty = 3,\t<span style=\"color: #008000;\"> #Define line type. 0 = blank, 3 = dotted<\/span><br \/>\nfont.axis = 4,\t<span style=\"color: #008000;\"> #Display axis tick labels in bold italics<\/span><br \/>\nfont.main = 3,\t<span style=\"color: #008000;\"> #Display the main title in italics<\/span><br \/>\ncex.axis = 1.2,\t\t <span style=\"color: #008000;\">#Make axis tick labels 1.2x larger than regular font<\/span><br \/>\ncex.main = 1.5,\t<span style=\"color: #008000;\"> #Make main title 1.5x larger than regular font size<\/span><br \/>\ncex.lab = 1.5,\t\t <span style=\"color: #008000;\">#Make axis title 1.5x larger than regular font size<\/span><br \/>\nbty = \"l\",\t\t <span style=\"color: #008000;\">#Specify which axes to draw (l = left + bottom)<\/span><br \/>\n<span style=\"color: #993300;\">mgp = c(2.1,1,0),<\/span> <span style=\"color: #008000;\"> #Specify axis label + tick locations relative to plot<\/span><br \/>\n)\t<span style=\"color: #008000;\"> #Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot9.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The axis titles have shifted closer to the axes.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nNext let&#8217;s add some color to the plot (it&#8217;s going to get ugly really quick). The various<br \/>\n<code>col<\/code> parameters modify the colors of the plot components. If you want to know all of the<br \/>\ncolors you can refer to by name, type <code>colors()<\/code> at the R command prompt.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\",  <span style=\"color: #008000;\">#X axis title<\/span><br \/>\nylab = \"Sample value\",<span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", \t\t <span style=\"color: #008000;\">#Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\ntype = \"b\",\t<span style=\"color: #008000;\"> #Plot points with lines between them.<\/span><br \/>\nlty = 3,\t\t <span style=\"color: #008000;\">#Define line type. 0 = blank, 3 = dotted<\/span><br \/>\nfont.axis = 4,\t<span style=\"color: #008000;\"> #Display axis tick labels in bold italics<\/span><br \/>\nfont.main = 3,\t\t <span style=\"color: #008000;\">#Display the main title in italics<\/span><br \/>\ncex.axis = 1.2,\t<span style=\"color: #008000;\"> #Make axis tick labels 1.2x larger than regular font<\/span><br \/>\ncex.main = 1.5,\t  \t <span style=\"color: #008000;\">#Make main title 1.5x larger than regular font size<\/span><br \/>\ncex.lab = 1.5,\t<span style=\"color: #008000;\"> #Make axis title 1.5x larger than regular font size<\/span><br \/>\nbty = \"l\",\t<span style=\"color: #008000;\"> #Specify which axes to draw (l = left + bottom)<\/span><br \/>\nmgp = c(2.1,1,0),\t <span style=\"color: #008000;\">#Specify axis label + tick locations relative to plot<\/span><br \/>\n<span style=\"color: #993300;\">col = \"red\",<\/span> <span style=\"color: #008000;\">#Plot the points + lines in red<\/span><br \/>\n<span style=\"color: #993300;\">col.axis = \"blue\",<\/span> <span style=\"color: #008000;\">#Set the axis tick label color<\/span><br \/>\n<span style=\"color: #993300;\">col.lab = \"dark red\",<\/span> <span style=\"color: #008000;\"> #Set the axis title color<\/span><br \/>\n<span style=\"color: #993300;\">col.main = \"#CC00CC\",<\/span> <span style=\"color: #008000;\"> #Set the figure title color using hex values.<\/span><br \/>\n<span style=\"color: #008000;\">#Hex values are simply an alternate way to specify a<br \/>\n#color.<\/span><br \/>\n)\t\t\t <span style=\"color: #008000;\">#Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot10.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">Those are some horrible colors.<\/figcaption><\/figure>\n<p>There are lots of other parameters you can modify in your figure. You can find out about<br \/>\nall the other parameters by typing <code>? par<\/code> at the R command prompt.<br \/>\n<img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>One of the other things you might want to do is insert special characters in the axis<br \/>\ntitles. For instance, maybe the units of our y-axis are actually degrees Celsius. Getting<br \/>\na degree symbol is slightly more involved, but here&#8217;s one way to do it:<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\",  <span style=\"color: #008000;\">#X axis title<\/span><br \/>\n<span style=\"color: #993300;\">ylab = expression(paste(\"Temp, \",degree,\"C\")),<\/span> <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", <span style=\"color: #008000;\"> #Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\n)\t<span style=\"color: #008000;\"> #Closing parenthesis for plot command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot11.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The y-axis label now has a fancy degree sign in it.<\/figcaption><\/figure>\n<p>In the code above, the <code> ylab = expression(paste(\"Temp, \",degree,\"C\")),<\/code> is doing something exciting. Using <code>expression<\/code> tells R to evaluate the code inside the parentheses. Inside those parentheses, we have a <code>paste<\/code> command, which tells R to stick the items inside paste&#8217;s parentheses together in a string. This combination of commands instructs R to create a string, but evaluate any special code words first. In this case, the code word is <code>degree<\/code>, which R turns into a degree symbol and sticks in between the <code>\"Temp, \"<\/code> and <code>\"C\"<\/code> strings.<br \/>\n<img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>There may also be instances where you want one of your axes to have different fonts,<br \/>\ncolors, size etc. This is accomplished by first suppressing the text for that axis in the<br \/>\nplot command using the <code>yaxt<\/code> or <code>xaxt<\/code> parameters, and then using a subsequent command<br \/>\nlike <code>axis<\/code> to modify the desired axis. In the example below, we&#8217;re going to suppress the<br \/>\ny-axis using the <code>yaxt = \"n\"<\/code> parameter inside the plot command. Then we&#8217;ll<br \/>\nfollow the plot command with the <code>axis<\/code> command wherein we lay out the parameters of the<br \/>\ny-axis. When the plot command is called, R still calculates where to put the y-axis<br \/>\nlabels and what their values should be, and it holds these in memory so that you can use<br \/>\nthem with the <code>axis<\/code> command.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\",  <span style=\"color: #008000;\">#X axis title<\/span><br \/>\nylab = expression(paste(\"Temp, \",degree,\"C\")),   <span style=\"color: #008000;\">#Y axis title<\/span><br \/>\nmain = \"Test\", <span style=\"color: #008000;\"> #Main title of plot<\/span><br \/>\nlas = 1,\t<span style=\"color: #008000;\"> #Rotate all tick labels horizontal<\/span><br \/>\n<span style=\"color: #993300;\">yaxt = \"n\",<\/span> <span style=\"color: #008000;\">#Suppress y-axis tick labels and title<\/span><br \/>\n)\t<span style=\"color: #008000;\"> #Closing parenthesis for plot command<\/span><br \/>\n<\/code><code><br \/>\n<span style=\"color: #993300;\">axis(2,<\/span> <span style=\"color: #008000;\">#Define which axis to modify. 1 = bottom x-axis, 2 = left y-axis, 3 = top etc...<\/span><br \/>\n<span style=\"color: #993300;\">las = 1,<\/span> <span style=\"color: #008000;\"> #Set tick label orientation (1 = horizontal)<\/span><br \/>\n<span style=\"color: #993300;\">cex.axis = 0.8,<\/span> <span style=\"color: #008000;\"> #Set font size (0.8 times current font size)<\/span><br \/>\n<span style=\"color: #993300;\">font.axis = 3,<\/span> <span style=\"color: #008000;\"> #Set font style (bold,italics etc, 3 = italics)<\/span><br \/>\n<span style=\"color: #993300;\">col.axis = \"tan4\",<\/span> <span style=\"color: #008000;\">#Set axis color<\/span><br \/>\n<span style=\"color: #993300;\">)<\/span> <span style=\"color: #008000;\"> #Closing parenthesis for axis command<\/span><br \/>\n<\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot12.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">The format of the y-axis tick labels is different than the x-axis tick labels.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Finally, you can drop text notations on the plot using the <code>text<\/code> command and specifying<br \/>\nthe x,y location of the text. Again we&#8217;ll use a greek symbol here.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1), <span style=\"color: #008000;\">#x and y data for our example plot<\/span><br \/>\nxlab = \"Sample number\", <span style=\"color: #008000;\"> #X axis title<\/span><br \/>\nylab = expression(paste(\"Temp, \",degree,\"C\")), <span style=\"color: #008000;\"> #Y axis title<\/span><br \/>\nmain = \"Test\", <span style=\"color: #008000;\"> #Main title of plot<\/span><br \/>\nlas = 1,\t\t <span style=\"color: #008000;\">#Rotate all tick labels horizontal<\/span><br \/>\nyaxt = \"n\",\t<span style=\"color: #008000;\"> #Suppress y-axis tick labels and title<\/span><br \/>\n)\t<span style=\"color: #008000;\"> #Closing parenthesis for plot command<\/span><\/code><br \/>\n<code><br \/>\n<span style=\"color: #993300;\">text(2,2,expression(paste(Delta,\"T\")))<\/span><\/code><\/p>\n<figure style=\"width: 216px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/base_plot13.png\" alt=\"\" width=\"216\" height=\"216\" \/><figcaption class=\"wp-caption-text\">Note the text label at (2,2) on the plot.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You can construct more elaborate labels using the facilities found in the R help for <code>plotmath<\/code>. For example, we can construct a y-axis label with a combination of normal text, italics, a greek symbol, and some superscripts using the <code>plain()<\/code> and <code>italic()<\/code> functions (there are also bold and bolditalic options as well). In this case, we will construct the label before issuing the<br \/>\nplot command.<\/p>\n<p><code><br \/>\nmylab = expression(paste(plain('Final '),<br \/>\nitalic('per capita '),<br \/>\nplain('rate ('), <span style=\"color: #008000;\">#that is an opening parenthesis inside the quotes<\/span><br \/>\nplain(mu), <span style=\"color: #008000;\">#greek symbols have special code words<\/span><br \/>\nplain('g snail '^{-1}), <span style=\"color: #008000;\">#superscript goes outside the quotes<\/span><br \/>\nplain('day'^{-1}),<br \/>\nplain(')')  )) <span style=\"color: #008000;\">#there is a closing parenthesis in those quotes<\/span><br \/>\n<\/code><br \/>\nThe above command uses <code>expression()<\/code> to create an expression object called <code>mylab<\/code> that is stored in memory. Inside the <code>expression()<\/code> command, we use <code>paste()<\/code> to paste together several plain and italic text strings. Note that we surround <em>per capita<\/em> with an <code>italic()<\/code> command to italicize those words. The greek symbol mu is inserted by simply typing mu, without quote marks around it (similar to how we displayed the Delta symbol above). The superscript -1 symbols are written outside of quotes as well, but inside the <code>plain()<\/code> parentheses. The use of the ^{ } symbols around the -1 tells R that we want everything inside the curly braces to be superscript (this is essentially TeX syntax, if you&#8217;re familiar with it). To make a subscript, you would use square braces [ ], and surround text with quotes inside the braces.<\/p>\n<p>After defining that mess of a label, we once again create our basic plot and instead of supplying a string for the ylab argument, we simply supply the expression stored in &#8216;mylab&#8217; which is converted to text by R.<br \/>\n<code><br \/>\nplot(c(1,2,3,4,5,6),c(4,3,6,2,1,1),<br \/>\nxlab = 'Sample Number',<br \/>\nylab = mylab, <span style=\"color: #008000;\">#use mylab for the y-label. Do not put quotes around mylab.<\/span><br \/>\nlas = 1,<br \/>\n)<br \/>\n<\/code><\/p>\n<figure id=\"attachment_900\" aria-describedby=\"caption-attachment-900\" style=\"width: 387px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/plot_xx.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-900\" title=\"plot_xx\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/plot_xx.png\" alt=\"\" width=\"387\" height=\"378\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/plot_xx.png 387w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/05\/plot_xx-300x293.png 300w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/a><figcaption id=\"caption-attachment-900\" class=\"wp-caption-text\">The y-axis label contains a mix of plain and italics words, superscripts, and a greek symbol.<\/figcaption><\/figure>\n<p><img decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/themes\/miller_theme\/images\/hr.gif\" alt=\"\" width=\"600\" \/><br \/>\nFor even more info on graphing in R and general R usage, I have found the <a href=\"http:\/\/www.statmethods.net\/index.html\">Quick-R<\/a> site extremely informative. The sections on <a href=\"http:\/\/www.statmethods.net\/graphs\/index.html\">graphs<\/a> and <a href=\"http:\/\/www.statmethods.net\/advgraphs\/index.html\">advanced graphs<\/a> are quite helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a walk-through of some of the basics of customizing plots in R. These are all based on the graphics package that comes in the base installation of R. Let&#8217;s start by making a basic plot in R. In the code snippets below, green text behind a # sign is considered comments by R, [&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,16,85,17,7,58,84,83],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-axis-label","tag-graphics","tag-greek-symbol","tag-plotting","tag-r","tag-r-project","tag-subscript","tag-superscript"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/228","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=228"}],"version-history":[{"count":75,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":304,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions\/304"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}