{"id":689,"date":"2021-02-26T17:50:18","date_gmt":"2021-02-26T15:50:18","guid":{"rendered":"http:\/\/mon.uvic.cat\/statistical-pills\/?page_id=689"},"modified":"2021-02-26T19:19:46","modified_gmt":"2021-02-26T17:19:46","slug":"basic-plots-with-r","status":"publish","type":"page","link":"https:\/\/mon.uvic.cat\/statistical-pills\/basic-plots-with-r\/","title":{"rendered":"Basic plots with R"},"content":{"rendered":"<div id=\"toc\">\n<div><\/div>\n<div><span style=\"color: inherit;font-family: inherit;font-size: 38.5px;font-weight: bold\">Basic plots with R<strong><a href=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-687 alignright\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT.png\" alt=\"\" width=\"319\" height=\"62\" srcset=\"https:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT.png 1623w, https:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT-300x58.png 300w, https:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT-1024x199.png 1024w, https:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT-768x150.png 768w, https:\/\/mon.uvic.cat\/statistical-pills\/files\/2021\/02\/FCT-1536x299.png 1536w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/a><\/strong><\/span><\/div>\n<div>\n<h4 class=\"author\">Malu Calle<\/h4>\n<\/div>\n<div><\/div>\n<div id=\"toc_header\"><strong>Table of Contents<br \/>\n<\/strong><\/div>\n<div><\/div>\n<ul>\n<li>\n<h5><a href=\"#toc_0\">Line charts<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_1\">Dot plot<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_2\">Histograms<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_3\">Boxplot<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_4\">Scatter plots<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_5\">Multiple Data Sets on One Plot<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_6\">Multiple scatter plots<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_7\">Bar charts<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_8\">Multiple bar plot<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_9\">Important graphical functions and parameters<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_10\">Creating a symbols and color chart<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_11\">Saving Graphics to Files<\/a><\/h5>\n<\/li>\n<li>\n<h5><a href=\"#toc_12\">Exercices<\/a><\/h5>\n<\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"color: inherit;font-family: inherit;font-size: 38.5px;font-weight: bold\">Basic plots with R<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Some examples in this entry have been adapted from \u201cProducing Simple Graphs with R\u201d by Frank McCown: <a href=\"http:\/\/www.harding.edu\/fmccown\/r\/\" rel=\"nofollow\">http:\/\/www.harding.edu\/fmccown\/r\/<\/a><\/p>\n<p><strong>Generation of random data for the examples<\/strong><\/p>\n<p>We generate a vector x of 15 values from a normal random distribution with mean=0 and standard deviation=1 and a vector y of 15 values from a normal random distribution with mean=0.2 and standard deviation=1:<\/p>\n<pre><code class=\"r\">set.seed(123)\r\nx&lt;-rnorm(15,0,1)\r\ni&lt;-c(1:15)\r\nset.seed(4)\r\ny&lt;-rnorm(15,0.2,1)\r\n<\/code><\/pre>\n<h2 id=\"toc_0\">Line charts<\/h2>\n<p>The simplest graphical representation of a numerical variable is the line chart provided by the command <code>plot()<\/code><\/p>\n<pre><code class=\"r\">plot(x)  #plots the values of x (vertical axis) as a function of the index of each value (horizontal axis)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-2-1-3.png\" alt=\"plot of chunk unnamed-chunk-2\" \/><\/p>\n<p><code>col<\/code>: specifies the color of the points<\/p>\n<ul>\n<li>col=1 black<\/li>\n<li>col=2 red<\/li>\n<li>col=3 green<\/li>\n<li>col=4 blue<\/li>\n<li>col=5 light blue<\/li>\n<\/ul>\n<pre><code class=\"r\">plot(x,col=3) #color=green\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-3-1-4.png\" alt=\"plot of chunk unnamed-chunk-3\" \/><\/p>\n<p><code>pch<\/code>: specifies the symbol for the points<\/p>\n<p>pch=1 symbol=cercle<\/p>\n<p>pch=2 symbol=triangle<\/p>\n<p>pch=3 symbol=plus<\/p>\n<p>pch=4 symbol=star<\/p>\n<p>pch=5 symbol=dyamond<\/p>\n<pre><code class=\"r\">plot(x,col=3, pch=4) # color=green, symbol=star\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-4-1-4.png\" alt=\"plot of chunk unnamed-chunk-4\" \/><\/p>\n<p><code>main<\/code> adds a title to the plot. <code>main<\/code> is specified within the <code>plot()<\/code> function<\/p>\n<pre><code class=\"r\">plot(x,col=3, main=\"line chart\") #color=green\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-5-1-4.png\" alt=\"plot of chunk unnamed-chunk-5\" \/><\/p>\n<p><code>title<\/code> adds a title to the plot. <code>title<\/code> is specified outside the <code>plot()<\/code> function<\/p>\n<pre><code class=\"r\">plot(x,col=3)\r\ntitle(\"line chart\")\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-6-1-4.png\" alt=\"plot of chunk unnamed-chunk-6\" \/><\/p>\n<p><code>type<\/code>: You can add lines between the points of different types<\/p>\n<pre><code class=\"r\">plot(x, type=\"o\")  # add lines between points\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-7-1-4.png\" alt=\"plot of chunk unnamed-chunk-7\" \/><\/p>\n<pre><code class=\"r\">plot(x, type=\"b\")  # add lines between points without touching the points\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-8-1-4.png\" alt=\"plot of chunk unnamed-chunk-8\" \/><\/p>\n<p><code>xlab<\/code> specifies the label of the x axis<\/p>\n<pre><code class=\"r\">plot(x, type=\"o\", xlab=\"this is the x label\")\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-9-1-4.png\" alt=\"plot of chunk unnamed-chunk-9\" \/><\/p>\n<pre><code class=\"r\">plot(x, type=\"o\", xlab=\"\")  # removes the x lable\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-10-1-4.png\" alt=\"plot of chunk unnamed-chunk-10\" \/><\/p>\n<p><code>points()<\/code>: add additional points to an existing plot<\/p>\n<pre><code class=\"r\">plot(x, type=\"o\", xlab=\"\")  # removes the x lable\r\npoints(y, col=2)      # add points y in red (col=2)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-11-1-4.png\" alt=\"plot of chunk unnamed-chunk-11\" \/><\/p>\n<p><code>lines()<\/code>: add points and lines to an existing plot<\/p>\n<pre><code class=\"r\">plot(x, type=\"o\", xlab=\"\")  # removes the x lable\r\nlines(y, type=\"o\", col=2)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-12-1-4.png\" alt=\"plot of chunk unnamed-chunk-12\" \/><\/p>\n<p><code>ylim<\/code>: define the limits of y axis<\/p>\n<pre><code class=\"r\">plot(x, type=\"o\", xlab=\"\", ylim=c(-3, 3))\r\nlines(y, type=\"o\", col=2)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-13-1-4.png\" alt=\"plot of chunk unnamed-chunk-13\" \/><\/p>\n<p><code>range<\/code>: range(x,y)=(min(x,y), max(x,y)) among two samples x and y<\/p>\n<pre><code class=\"r\">range(x,y)\r\n<\/code><\/pre>\n<pre>## [1] -1.265061  2.096540\r\n<\/pre>\n<pre><code class=\"r\">plot(x, type=\"o\", xlab=\"\", ylim=range(x,y))\r\nlines(y, type=\"o\", col=2)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-14-1-4.png\" alt=\"plot of chunk unnamed-chunk-14\" \/><\/p>\n<h2 id=\"toc_1\">Dot plot<\/h2>\n<p><code>dotchart<\/code> provides a dot plot of a numerical variable. A dot plot is similar to a line chart but the values of the variable are in the horizontal axis and the indeces in the vertical axis<\/p>\n<pre><code class=\"r\">dotchart(x, labels=i)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-15-1-4.png\" alt=\"plot of chunk unnamed-chunk-15\" \/><\/p>\n<p>The following dot plot provides a graphical representation of the ranking of variable x<\/p>\n<pre><code class=\"r\">dotchart(x[order(x)], labels=order(x))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-16-1-4.png\" alt=\"plot of chunk unnamed-chunk-16\" \/><\/p>\n<h2 id=\"toc_2\">Histograms<\/h2>\n<p><code>hist()<\/code>: The histogram is one of the main important plots of a numerical variable<\/p>\n<pre><code class=\"r\">hist(x)  # title and x label are included by default\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-17-1-4.png\" alt=\"plot of chunk unnamed-chunk-17\" \/><\/p>\n<pre><code class=\"r\">hist(x, col=5)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-18-1-4.png\" alt=\"plot of chunk unnamed-chunk-18\" \/><\/p>\n<h2 id=\"toc_3\">Boxplot<\/h2>\n<p><code>boxplot()<\/code><\/p>\n<pre><code class=\"r\">boxplot(x)\r\ntitle(\"boxplot of x\", ylab=\"x\")\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-19-1-4.png\" alt=\"plot of chunk unnamed-chunk-19\" \/><\/p>\n<pre><code class=\"r\">boxplot(x, horizontal=T)\r\ntitle(\"boxplot of x\", xlab=\"x\")\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-20-1-4.png\" alt=\"plot of chunk unnamed-chunk-20\" \/><\/p>\n<p>This is an example of a plot containing both a histogram and a boxplot:<\/p>\n<pre><code class=\"r\">hist(x,main='histogram and boxplot of x',xlab='x')\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-21-1-4.png\" alt=\"plot of chunk unnamed-chunk-21\" \/><\/p>\n<p><code>ylim<\/code>: range of the y axis<\/p>\n<p>we need to extend the y axis in order to make room for the boxplot<\/p>\n<pre><code class=\"r\">hist(x,main='histogram and boxplot of x',xlab='x',ylim=c(0,12))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-22-1-4.png\" alt=\"plot of chunk unnamed-chunk-22\" \/><\/p>\n<p><code>add=T<\/code> : this allows the addition of the boxplot in the histogram<\/p>\n<p><code>axes=F<\/code>: we remove the axes of the boxplot<\/p>\n<pre><code class=\"r\">hist(x,main='histogram and boxplot of x',xlab='x',ylim=c(0,12))\r\nboxplot(x,horizontal=TRUE,at=10,add=TRUE,axes=FALSE)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-23-1-4.png\" alt=\"plot of chunk unnamed-chunk-23\" \/><\/p>\n<p><code>boxwex<\/code>: specifies the width of the box<\/p>\n<pre><code class=\"r\">hist(x,main='histogram and boxplot of x',xlab='x',ylim=c(0,12))\r\nboxplot(x,horizontal=TRUE,at=10,add=TRUE,axes=FALSE, boxwex = 5)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-24-1-4.png\" alt=\"plot of chunk unnamed-chunk-24\" \/><\/p>\n<h2 id=\"toc_4\">Scatter plots<\/h2>\n<p><code>plot()<\/code> applied to two variables provides a scatter plot<\/p>\n<pre><code class=\"r\">y&lt;-x^2   # Define y as the squared values of x\r\nplot(x,y)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-25-1-4.png\" alt=\"plot of chunk unnamed-chunk-25\" \/><\/p>\n<pre><code class=\"r\">plot(x,y, main=\"scatter plot x and y\")\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-26-1-4.png\" alt=\"plot of chunk unnamed-chunk-26\" \/><\/p>\n<h2 id=\"toc_5\">Multiple Data Sets on One Plot<\/h2>\n<p><code>points()<\/code>: add additional points to an existing plot<\/p>\n<pre><code class=\"r\">set.seed(123)\r\nx&lt;-rnorm(15,0,1)\r\ny&lt;-x^2\r\nplot(x,y)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-27-1-4.png\" alt=\"plot of chunk unnamed-chunk-27\" \/><\/p>\n<pre><code class=\"r\">plot(x,y)\r\nx1 &lt;- c(-1,1)  # we add points x=1 and x=-1\r\ny1 &lt;- x1^2\r\npoints(x1,y1,col=2)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-28-1-4.png\" alt=\"plot of chunk unnamed-chunk-28\" \/><\/p>\n<pre><code class=\"r\">plot(x,y)\r\npoints(x1,y1,col=2, pch=3) # symbol=plus\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-29-1-4.png\" alt=\"plot of chunk unnamed-chunk-29\" \/><\/p>\n<p>We add lables to the points<\/p>\n<pre><code class=\"r\">plot(x,y)\r\npoints(x1,y1,col=2, pch=3) # symbol=plus\r\ntext(x1+0.1, y1+0.2, col=2, c(\"A\", \"B\"))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-30-1-4.png\" alt=\"plot of chunk unnamed-chunk-30\" \/><\/p>\n<pre><code class=\"r\">plot(x,y)\r\npoints(x1,y1,col=2, pch=3) # symbol=plus\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-31-1-4.png\" alt=\"plot of chunk unnamed-chunk-31\" \/><\/p>\n<p><code>legend<\/code>: x and y coordinates on the plot to place the legend followed by a list of labels to use<\/p>\n<pre><code class=\"r\">plot(x,y)\r\nlegend(-1,2,c(\"Original\",\"new\"),col=c(1,2),pch=c(1,4))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-32-1-4.png\" alt=\"plot of chunk unnamed-chunk-32\" \/><\/p>\n<p><code>col.main<\/code>: specifies the color of the title<\/p>\n<pre><code class=\"r\">plot(x,y)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\nlegend(-1,3,c(\"Original\",\"new\"),col=c(1,2),pch=c(1,4))\r\ntitle(\"scatter plot\", col.main=3)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-33-1-4.png\" alt=\"plot of chunk unnamed-chunk-33\" \/><\/p>\n<p><code>font.main<\/code>: specifies the font of the title<\/p>\n<pre><code class=\"r\">plot(x,y)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\nlegend(-1,3,c(\"Original\",\"new\"),col=c(1,2),pch=c(1,4))\r\ntitle(\"scatter plot\", font.main=3)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-34-1-4.png\" alt=\"plot of chunk unnamed-chunk-34\" \/><\/p>\n<p><code>cex()<\/code>: Proportion of reduction or amplification of a font<\/p>\n<pre><code class=\"r\">plot(x,y)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\nlegend(-1,3,c(\"Original\",\"new\"),col=c(1,2),pch=c(1,4), cex=0.7)\r\n#font.main: specifies the font of the title\r\ntitle(\"scatter plot\", font.main=3)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-35-1-4.png\" alt=\"plot of chunk unnamed-chunk-35\" \/><\/p>\n<p><code>axes=F<\/code>: remove axes<\/p>\n<p><code>axis()<\/code>: define new axes<\/p>\n<pre><code class=\"r\">plot(x,y,axes=FALSE)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\naxis(1,pos=c(-0.5,0),at=seq(-2,2,by=0.4))\r\naxis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-36-1-4.png\" alt=\"plot of chunk unnamed-chunk-36\" \/><\/p>\n<p><code>ann=F<\/code>: removes annotation<\/p>\n<p><code>lab<\/code>: define new lables<\/p>\n<pre><code class=\"r\">plot(x,y,axes=FALSE, ann=F)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\naxis(1,pos=c(-0.5,0),at=seq(min(x),max(x),by=0.8), lab=c(\"a\", \"b\", \"c\", \"d\"))\r\naxis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-37-1-4.png\" alt=\"plot of chunk unnamed-chunk-37\" \/><\/p>\n<p><code>abline()<\/code>: abline(a,b) adds line y=a+bx<\/p>\n<p><code>lty<\/code>: type of line<\/p>\n<pre><code class=\"r\">plot(x,y,axes=FALSE, ann=F)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\naxis(1,pos=c(-0.5,0),at=seq(min(x),max(x),by=0.8), lab=c(\"a\", \"b\", \"c\", \"d\"))\r\naxis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))\r\nabline(1, -0.7, lty=3)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-38-1-4.png\" alt=\"plot of chunk unnamed-chunk-38\" \/><\/p>\n<p><code>box()<\/code>: creates a box around the plot<\/p>\n<pre><code class=\"r\">plot(x,y,axes=FALSE, ann=F)\r\npoints(x1,y1,col=2, pch=4) # symbol=star\r\naxis(1,pos=c(-0.5,0),at=seq(min(x),max(x),by=0.8), lab=c(\"a\", \"b\", \"c\", \"d\"))\r\naxis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))\r\nabline(1, -0.7, lty=3)\r\nbox()\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-39-1-4.png\" alt=\"plot of chunk unnamed-chunk-39\" \/><\/p>\n<h2 id=\"toc_6\">Multiple scatter plots<\/h2>\n<p><code>pairs()<\/code>: Scatter plots of all pair of variables in a data frame<\/p>\n<pre><code class=\"r\">set.seed(123)\r\ndata&lt;-data.frame(x1=runif(10), x2=runif(10), x3=runif(10), x4=runif(10))\r\npairs(data)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-40-1-4.png\" alt=\"plot of chunk unnamed-chunk-40\" \/><\/p>\n<h2 id=\"toc_7\">Bar charts<\/h2>\n<p><code>barplot()<\/code><\/p>\n<pre><code class=\"r\">group&lt;-c(\"A\",\"B\",\"C\")\r\nfreq&lt;-c(20, 50, 30)\r\nbarplot(freq)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-41-1-4.png\" alt=\"plot of chunk unnamed-chunk-41\" \/><\/p>\n<p><code>names.arg<\/code><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-42-1-4.png\" alt=\"plot of chunk unnamed-chunk-42\" \/><\/p>\n<p><code>density<\/code><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group, density=c(5,30,70))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-43-1-4.png\" alt=\"plot of chunk unnamed-chunk-43\" \/><\/p>\n<p><code>border<\/code><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group, density=c(5,30,70), border=3)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-44-1-4.png\" alt=\"plot of chunk unnamed-chunk-44\" \/><\/p>\n<h2 id=\"toc_8\">Multiple bar plot<\/h2>\n<pre><code class=\"r\">group&lt;-c(\"A\",\"B\",\"C\")\r\nfreq1&lt;-c(20, 50, 30)\r\nfreq2&lt;-c(40, 20, 10)\r\nfreq&lt;-rbind(freq1,freq2)\r\nfreq\r\n<\/code><\/pre>\n<pre>##       [,1] [,2] [,3]\r\n## freq1   20   50   30\r\n## freq2   40   20   10\r\n<\/pre>\n<p><code>names.arg<\/code><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-46-1-3.png\" alt=\"plot of chunk unnamed-chunk-46\" \/><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group, col=c(3,4))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-47-1-4.png\" alt=\"plot of chunk unnamed-chunk-47\" \/><\/p>\n<p><code>beside<\/code><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group, beside=TRUE)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-48-1-4.png\" alt=\"plot of chunk unnamed-chunk-48\" \/><\/p>\n<pre><code class=\"r\">barplot(freq, names.arg=group, beside=TRUE, col=c(2,3))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-49-1-4.png\" alt=\"plot of chunk unnamed-chunk-49\" \/><\/p>\n<h2 id=\"toc_9\">Important graphical functions and parameters<\/h2>\n<p><code>par()<\/code>: set graphical parameters<\/p>\n<p>Before you change the graphical parameters it is convenient to store the default values<\/p>\n<pre><code class=\"r\">defaultpar&lt;-par()\r\n<\/code><\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"r\">par(mfrow=c(2,3)) # puts 6 pictures in a plot distributed in 2 rows and 3 columns\r\nhist(data$x1, main=\"Histogram x1\")\r\nhist(data$x2, main=\"Histogram x2\")\r\nhist(data$x3, main=\"Histogram x3\")\r\nboxplot(data$x1, main=\"Boxplot x1\")\r\nboxplot(data$x2, main=\"Boxplot x2\")\r\nboxplot(data$x3, main=\"Boxplot x3\")\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-51-1-3.png\" alt=\"plot of chunk unnamed-chunk-51\" \/><\/p>\n<pre><code class=\"r\">par(defaultpar)  # reset the default graphical parameters\r\n<\/code><\/pre>\n<p><strong>Important arguments:<\/strong><\/p>\n<p><code>mfrow<\/code>: number of pictures per row and column in a plot<\/p>\n<p><code>mar<\/code>: specifies the margin sizes around the plotting area in order: c(bottom, left, top, right)<\/p>\n<p><code>col<\/code>: color of symbols<\/p>\n<p><code>pch<\/code>: type of symbols, samples: example(points)<\/p>\n<p><code>lwd<\/code>: size of symbols<\/p>\n<p><code>cex.*<\/code>: control font sizes<\/p>\n<h2 id=\"toc_10\">Creating a symbols and color chart<\/h2>\n<p>Make an empty chart<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-53-1-3.png\" alt=\"plot of chunk unnamed-chunk-53\" \/><\/p>\n<p>Plot digits 0-4 with increasing size and color<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-54-1-4.png\" alt=\"plot of chunk unnamed-chunk-54\" \/><\/p>\n<p>Plot symbols 0-4 with increasing size and color<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\n\r\npoints(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)\r\ntext((1:5)+0.4, rep(5,5), cex=0.6, (0:4))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-55-1-4.png\" alt=\"plot of chunk unnamed-chunk-55\" \/><\/p>\n<p>Plot symbols 5-9 with labels<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\npoints(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)\r\ntext((1:5)+0.4, rep(5,5), cex=0.6, (0:4))\r\n\r\npoints(1:5, rep(4,5), cex=2, pch=(5:9))\r\ntext((1:5)+0.4, rep(4,5), cex=0.6, (5:9))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-56-1-4.png\" alt=\"plot of chunk unnamed-chunk-56\" \/><\/p>\n<p>Plot symbols 10-14 with labels<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\npoints(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)\r\ntext((1:5)+0.4, rep(5,5), cex=0.6, (0:4))\r\npoints(1:5, rep(4,5), cex=2, pch=(5:9))\r\ntext((1:5)+0.4, rep(4,5), cex=0.6, (5:9))\r\n\r\npoints(1:5, rep(3,5), cex=2, pch=(10:14))\r\ntext((1:5)+0.4, rep(3,5), cex=0.6, (10:14))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-57-1-4.png\" alt=\"plot of chunk unnamed-chunk-57\" \/><\/p>\n<p>Plot symbols 15-19 with labels<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\npoints(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)\r\ntext((1:5)+0.4, rep(5,5), cex=0.6, (0:4))\r\npoints(1:5, rep(4,5), cex=2, pch=(5:9))\r\ntext((1:5)+0.4, rep(4,5), cex=0.6, (5:9))\r\npoints(1:5, rep(3,5), cex=2, pch=(10:14))\r\ntext((1:5)+0.4, rep(3,5), cex=0.6, (10:14))\r\n\r\npoints(1:5, rep(2,5), cex=2, pch=(15:19))\r\ntext((1:5)+0.4, rep(2,5), cex=0.6, (15:19))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-58-1-4.png\" alt=\"plot of chunk unnamed-chunk-58\" \/><\/p>\n<p>Plot symbols 20-25 with labels<\/p>\n<pre><code class=\"r\">plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\npoints(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)\r\ntext((1:5)+0.4, rep(5,5), cex=0.6, (0:4))\r\npoints(1:5, rep(4,5), cex=2, pch=(5:9))\r\ntext((1:5)+0.4, rep(4,5), cex=0.6, (5:9))\r\npoints(1:5, rep(3,5), cex=2, pch=(10:14))\r\ntext((1:5)+0.4, rep(3,5), cex=0.6, (10:14))\r\npoints(1:5, rep(2,5), cex=2, pch=(15:19))\r\ntext((1:5)+0.4, rep(2,5), cex=0.6, (15:19))\r\n\r\npoints((1:6)*0.8+0.2, rep(1,6), cex=2, pch=(20:25))\r\ntext((1:6)*0.8+0.5, rep(1,6), cex=0.6, (20:25))\r\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-59-1-4.png\" alt=\"plot of chunk unnamed-chunk-59\" \/><\/p>\n<h2 id=\"toc_11\">Saving Graphics to Files<\/h2>\n<p><code>pdf()<\/code> redirect the plots to a pdf file. Similarly: <code>jpeg<\/code>, <code>png<\/code>, <code>ps<\/code>, <code>tiff<\/code><\/p>\n<p><code>dev.off()<\/code> shuts down the specified device<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"r\">pdf(\"color_chart.pdf\")  # creates a pdf file containing the following plot, a color chart\r\n\r\nplot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type=\"n\", ann=FALSE)\r\ntext(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)\r\npoints(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)\r\ntext((1:5)+0.4, rep(5,5), cex=0.6, (0:4))\r\npoints(1:5, rep(4,5), cex=2, pch=(5:9))\r\ntext((1:5)+0.4, rep(4,5), cex=0.6, (5:9))\r\npoints(1:5, rep(3,5), cex=2, pch=(10:14))\r\ntext((1:5)+0.4, rep(3,5), cex=0.6, (10:14))\r\npoints(1:5, rep(2,5), cex=2, pch=(15:19))\r\ntext((1:5)+0.4, rep(2,5), cex=0.6, (15:19))\r\n\r\npoints((1:6)*0.8+0.2, rep(1,6), cex=2, pch=(20:25))\r\ntext((1:6)*0.8+0.5, rep(1,6), cex=0.6, (20:25))\r\n\r\ndev.off()\r\n<\/code><\/pre>\n<h2 id=\"toc_12\">Exercices<\/h2>\n<p>Create the following plots with R using data from \u201cSNPdataset.txt\u201d:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-61-1-3.png\" alt=\"plot of chunk unnamed-chunk-61\" \/><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-61-2-3.png\" alt=\"plot of chunk unnamed-chunk-61\" \/><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-61-3.png\" alt=\"plot of chunk unnamed-chunk-61\" \/><img decoding=\"async\" src=\"http:\/\/mon.uvic.cat\/statistical-pills\/files\/2018\/05\/unnamed-chunk-61-4.png\" alt=\"plot of chunk unnamed-chunk-61\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basic plots with R Malu Calle Table of Contents Line charts Dot plot Histograms Boxplot Scatter plots Multiple Data Sets &hellip; <a href=\"https:\/\/mon.uvic.cat\/statistical-pills\/basic-plots-with-r\/\">Read More<\/a><\/p>\n","protected":false},"author":196,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-689","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/pages\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/users\/196"}],"replies":[{"embeddable":true,"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":3,"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/pages\/689\/revisions"}],"predecessor-version":[{"id":752,"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/pages\/689\/revisions\/752"}],"wp:attachment":[{"href":"https:\/\/mon.uvic.cat\/statistical-pills\/wp-json\/wp\/v2\/media?parent=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}