<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=R_-_Graphics</id>
	<title>R - Graphics - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=R_-_Graphics"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=R_-_Graphics&amp;action=history"/>
	<updated>2026-05-14T01:05:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=R_-_Graphics&amp;diff=52413&amp;oldid=prev</id>
		<title>Bernard Szlachta: /* XY Plot */</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=R_-_Graphics&amp;diff=52413&amp;oldid=prev"/>
		<updated>2017-02-15T03:51:12Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;XY Plot&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Intro to R|055]]&lt;br /&gt;
&lt;br /&gt;
== Bar chart ==&lt;br /&gt;
Let us start with a simple bar chart graphing salary from the emp table.&lt;br /&gt;
 emp &amp;lt;- read.table(&amp;quot;https://training-course-material.com/images/9/97/Emp.txt&amp;quot;,header=TRUE)&lt;br /&gt;
 barplot(emp$SAL)&lt;br /&gt;
[[File:R-bar-chart.PNG|R-bar-chart.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can define the title of the graph with &amp;#039;main&amp;#039; function.&lt;br /&gt;
 barplot(emp$SAL, main=&amp;quot;Salary Bar Chart&amp;quot;)&lt;br /&gt;
[[File:R-bar-chart1.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can also label Y-Axis using &amp;#039;ylab&amp;#039; function and extract the X-Axis values from the ENAME column using &amp;#039;names&amp;#039; function.&lt;br /&gt;
 barplot(emp$SAL, main=&amp;quot;Salary Bar Chart&amp;quot;, ylab=&amp;quot;Salary&amp;quot;, names.arg=emp$ENAME, las=2)&lt;br /&gt;
[[File:R-bar-chart2.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s make it pretty by using some color.&lt;br /&gt;
 barplot(emp$SAL, main=&amp;quot;Salary Bar Chart&amp;quot;, ylab=&amp;quot;Salary&amp;quot;, names.arg=emp$ENAME, las=2, col=rainbow(15))&lt;br /&gt;
[[File:R-bar-chart3.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
== Legend ==&lt;br /&gt;
&lt;br /&gt;
 l &amp;lt;- list(MANAGER=2,&lt;br /&gt;
           CLERK=3,&lt;br /&gt;
           SALESMAN=4,&lt;br /&gt;
           PERSIDENT=5,&lt;br /&gt;
           ANALYST=6)&lt;br /&gt;
 jobcolors &amp;lt;- as.integer(l[emp$JOB]);&lt;br /&gt;
 barplot(emp$SAL,names.arg = emp$ENAME, las=2&lt;br /&gt;
          ,col=jobcolors)&lt;br /&gt;
 legend(&amp;quot;top&amp;quot;, &lt;br /&gt;
        legend = names(l), &lt;br /&gt;
        fill = as.integer(l))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can also generate colours automatically&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SPlus&amp;quot;&amp;gt;&lt;br /&gt;
jobs = as.character(unique(emp$JOB))&lt;br /&gt;
noofcol=length(jobs)&lt;br /&gt;
cols_no &amp;lt;- rainbow(noofcol)&lt;br /&gt;
cols = cols_no;&lt;br /&gt;
# list l should have $MANAGER -&amp;gt; FF0000FF, $PRESIDENT -&amp;gt; ......&lt;br /&gt;
names(cols) &amp;lt;- jobs&lt;br /&gt;
position_cols &amp;lt;- cols[as.character(emp$JOB)]&lt;br /&gt;
barplot(emp$SAL,names.arg = emp$ENAME, las=2&lt;br /&gt;
        ,col=position_cols)&lt;br /&gt;
legend(&amp;quot;right&amp;quot;, &lt;br /&gt;
       legend = jobs, &lt;br /&gt;
       fill = cols_no&lt;br /&gt;
       )&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
:[[File:ClipCapIt-170215-034710.PNG]]&lt;br /&gt;
&lt;br /&gt;
== par function ==&lt;br /&gt;
# In the previous example, where the &amp;#039;&amp;#039;las&amp;#039;&amp;#039; paramter is commign from?&lt;br /&gt;
# If you try to type &amp;#039;&amp;#039;&amp;#039;?las&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# Las come from &amp;#039;&amp;#039;par&amp;#039;&amp;#039; function&lt;br /&gt;
# Stands for Label Axis Style&lt;br /&gt;
&lt;br /&gt;
== Color coding for JOB ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;Splus&amp;quot;&amp;gt;&lt;br /&gt;
 l &amp;lt;- list(MANAGER=2,&lt;br /&gt;
          CLERK=3,&lt;br /&gt;
          SALESMAN=4,&lt;br /&gt;
          PERSIDENT=5,&lt;br /&gt;
          ANALYST=6)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 barplot(emp$SAL,names.arg = emp$ENAME, las=2&lt;br /&gt;
         ,col=as.integer(l[emp$JOB]))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pie Chart ==&lt;br /&gt;
Let us make a simple Pie Chart graphing the salary from the emp table.&lt;br /&gt;
 pie(emp$SAL)&lt;br /&gt;
 TODO: chart&lt;br /&gt;
&lt;br /&gt;
It is pretty useless unless we have labels right&lt;br /&gt;
&lt;br /&gt;
Now let&amp;#039;s add a title, label with names of the employees and change the colors.&lt;br /&gt;
 pie(emp$SAL, main=&amp;quot;Salary Pie Chart&amp;quot;, labels=emp$ENAME, col=rainbow(15))&lt;br /&gt;
[[File:R-pie-chart1.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can also use percentage of the salary to label the pie chart.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SPlus&amp;quot;&amp;gt;&lt;br /&gt;
 SAL_labels &amp;lt;-  round(emp$SAL/sum(emp$SAL) * 100, 1)&lt;br /&gt;
 SAL_labels &amp;lt;- paste(SAL_labels,&amp;quot;%&amp;quot;)&lt;br /&gt;
 SAL_labels &amp;lt;- paste(SAL_labels,emp$ENAME) &lt;br /&gt;
 pie(emp$SAL, main=&amp;quot;Salary Pie Chart&amp;quot;, labels=SAL_labels, col=rainbow(15)) &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
:[[File:ClipCapIt-170215-035544.PNG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Table Function ===&lt;br /&gt;
Table function returns frequency of a factor&lt;br /&gt;
&amp;gt; table(emp$DEPTNO)&lt;br /&gt;
&lt;br /&gt;
 10 20 30 &lt;br /&gt;
  3  5  6 &lt;br /&gt;
&lt;br /&gt;
How many people work in specific departments&lt;br /&gt;
 par(mar=c(0,0,0,0))&lt;br /&gt;
 pie(table(emp$DEPTNO))&lt;br /&gt;
:[[File:ClipCapIt-160306-172127.PNG|300px]]&lt;br /&gt;
&lt;br /&gt;
== Box Plot ==&lt;br /&gt;
Box Plots graphically depict groups of numerical data through minimum, lower quartile (Q1), median (Q2), upper quartile (Q3) and maximum points.  &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;boxplot( )&amp;#039;&amp;#039; function is used to provide point identification, labels, and a formula interface for box plots without a grouping variable. &lt;br /&gt;
&lt;br /&gt;
Let us draw a simple box plot for salary.&lt;br /&gt;
 boxplot(emp$SAL)&lt;br /&gt;
[[File:R-boxplot.PNG|185px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can added the head, label and some colors.&lt;br /&gt;
 boxplot(emp$SAL, main=&amp;quot;Salary Box Plot&amp;quot;, ylab=&amp;quot;Salary&amp;quot;, col=c(&amp;quot;yellow&amp;quot;))&lt;br /&gt;
[[File:R-boxplot1.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Quantile-Comparison Plot ==&lt;br /&gt;
Quantile-Comparison Plot is a probability plot, a graphical way of comparing two probability distributions by plotting their quantiles against each other.&lt;br /&gt;
We can use &amp;#039;&amp;#039;qqPlot()&amp;#039;&amp;#039; function to create a Q-Q Plot.&lt;br /&gt;
 install.packages(&amp;quot;EnvStats&amp;quot;)&lt;br /&gt;
 library(EnvStats)&lt;br /&gt;
 qqPlot(emp$SAL, dist=&amp;quot;norm&amp;quot;)&lt;br /&gt;
[[File:R-qq-plot1.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us add the head, label for y axis and some colors.&lt;br /&gt;
 qqPlot(emp$SAL, dist=&amp;quot;norm&amp;quot;, main=&amp;quot;Salary Q-Q Plot&amp;quot;, ylab=&amp;quot;Salary&amp;quot;, col=rainbow(15))&lt;br /&gt;
[[File:R-qq-plot.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
== Plot ==&lt;br /&gt;
We can use &amp;#039;&amp;#039;plot()&amp;#039;&amp;#039; function for plotting of R objects.&lt;br /&gt;
&lt;br /&gt;
Let us use gpa table to create a simple Plot. &lt;br /&gt;
 gpa &amp;lt;- read.table(&amp;quot;https://training-course-material.com/images/8/86/Study-time-gpa.txt&amp;quot;,h=T)&lt;br /&gt;
&lt;br /&gt;
 plot(gpa)&lt;br /&gt;
[[File:R-plot.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
Now let us make it pretty.&lt;br /&gt;
 plot(gpa, main=&amp;quot;GPA-Houors Plot&amp;quot;, col=&amp;quot;hotpink&amp;quot;)&lt;br /&gt;
[[File:R-plot2.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
== Scatter Plot ==&lt;br /&gt;
We can use scatter plots when a variable exists that is under the influence of the experimenter. &lt;br /&gt;
&lt;br /&gt;
GPA is under the control of the hours on the gpa table so we can create the scatter plot using &amp;#039;&amp;#039;scaterplot()&amp;#039;&amp;#039; function.&lt;br /&gt;
&lt;br /&gt;
 library(&amp;#039;car&amp;#039;)&lt;br /&gt;
 scatterplot(GPA~Hours, reg.line=lm, smooth=TRUE, &lt;br /&gt;
             spread=TRUE, boxplots=&amp;#039;xy&amp;#039;, span=0.5, data=gpa)&lt;br /&gt;
[[File:R-scatterplot1.PNG|220px]]&lt;br /&gt;
&lt;br /&gt;
== XY Plot ==&lt;br /&gt;
 library(lattice)&lt;br /&gt;
 xyplot(GPA ~ Hours, pch=16,main=&amp;quot;GPA-Houors Plot&amp;quot;,  &lt;br /&gt;
        auto.key=list(border=TRUE), par.settings = simpleTheme(pch=16), &lt;br /&gt;
        scales=list(x=list(relation=&amp;#039;same&amp;#039;), y=list(relation=&amp;#039;same&amp;#039;)), data=gpa)&lt;br /&gt;
[[File:R-xy-plot.PNG|200px]]&lt;br /&gt;
&lt;br /&gt;
== Graph Exercise ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Use &amp;#039;sales&amp;#039; table and &amp;#039;gpa&amp;#039; table for the questions below&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 sales &amp;lt;- read.table(&amp;quot;https://training-course-material.com/images/8/8f/Sales.txt&amp;quot;,header=TRUE)&lt;br /&gt;
 gpa &amp;lt;- read.table(&amp;quot;https://training-course-material.com/images/8/86/Study-time-gpa.txt&amp;quot;,h=T)&lt;br /&gt;
&lt;br /&gt;
# Create a Barchart for the sales by country (use &amp;#039;sales&amp;#039; table). &lt;br /&gt;
# Create a Pie Chart with the percentage of the total sales by country (use &amp;#039;sales&amp;#039;) table. &lt;br /&gt;
# Create a Box Plot for the sales (use &amp;#039;sales&amp;#039; table).&lt;br /&gt;
# Create a Plot (use &amp;#039;gpa&amp;#039; table).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Answers ===&lt;br /&gt;
Answers are in source code&lt;br /&gt;
1. Create a Barchart for the sales by country. &lt;br /&gt;
&amp;lt;!-- barplot(sales$SALES, main=&amp;quot;Sales by Country&amp;quot;, ylab=&amp;quot;Sales&amp;quot;, names.arg=sales$NAME, las=2, col=rainbow(7)) --&amp;gt;&lt;br /&gt;
 [[File:R-exercise1.PNG|150px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2. Create a Pie Chart with the percentage of the total sales by country. &lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
 SALES_labels &amp;lt;-  round(sales$SALES/sum(sales$SALES) * 100, 1)&lt;br /&gt;
 SALES_labels &amp;lt;- paste(SALES_labels,&amp;quot;%&amp;quot;)&lt;br /&gt;
 pie(table(sales$SALES), main=&amp;quot;Sales by Country Pie Chart&amp;quot;, labels=SALES_labels, col=rainbow(7))&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
 [[File:R-exercise2.PNG|150px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3. Create a Box Plot for the sales.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
 boxplot(sales$SALES, main=&amp;quot;Sales Box Plot&amp;quot;,  ylab=&amp;quot;Sales&amp;quot;, col=c(&amp;quot;skyblue&amp;quot;))&lt;br /&gt;
 --&amp;gt;&lt;br /&gt;
 [[File:R-exercise3.PNG|170px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4. Create a Plot using the gpa table.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
 plot(gpa, main=&amp;quot;GPA-Houors Plot&amp;quot;, col=&amp;quot;hotpink&amp;quot;) &lt;br /&gt;
 --&amp;gt;&lt;br /&gt;
 [[File:R-plot2.PNG|170px]]&lt;/div&gt;</summary>
		<author><name>Bernard Szlachta</name></author>
	</entry>
</feed>