0% found this document useful (0 votes)
3 views6 pages

Graphics With R

The document provides an overview of graphical functions in R, detailing high-level and low-level plotting functions, as well as how to manage graphical devices. It explains the various types of graphical devices available, how to partition graphics, and includes examples of different plotting techniques and customization options. Additionally, it demonstrates the use of specific datasets to create various types of plots, including histograms, boxplots, and scatterplots.

Uploaded by

S Pushpalatha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Graphics With R

The document provides an overview of graphical functions in R, detailing high-level and low-level plotting functions, as well as how to manage graphical devices. It explains the various types of graphical devices available, how to partition graphics, and includes examples of different plotting techniques and customization options. Additionally, it demonstrates the use of specific datasets to create various types of plots, including histograms, boxplots, and scatterplots.

Uploaded by

S Pushpalatha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Graphics with R

demo(graphics)

demo(persp)

The result of a graphical function cannot be assigned to an object but is sent to a graphical
device.

A graphical device is a graphical window or a file.

There are two kinds of graphical functions:

1. The high-level plotting functions - which create a new graph,


2. The low-level plotting functions – which add elements to an existing graph.

The graphs are produced with respect to graphical parameters which are defined by default and
can be modified with the function par.

4.1 Managing graphics


4.1.1 Opening several graphical devices

When a graphical function is executed,


If no graphical device is open, R opens a graphical window and displays the graph.
A graphical device may be open with an appropriate function.
The list of available graphical devices depends on the operating system.
The graphical windows are called X11 under Unix/Linux and windows under Windows.
X11()
Windows()
A graphical device which is a file will be open with a function depending on the format:
postscript(), pdf(), png(),

The following graphics devices are currently available:

 windows The graphics device for Windows (on screen, to printer and to Windows
metafile).
 pdf Write PDF graphics commands to a file
 postscript Writes PostScript graphics commands to a file
 xfig Device for XFIG graphics file format
 bitmap bitmap pseudo-device via Ghostscript (if available).

 pictex Writes TeX/PicTeX graphics commands to a file (of historical interest only)
 cairo_pdf, cairo_ps PDF and PostScript devices based on cairo graphics.
 svg SVG device based on cairo graphics.
 png PNG bitmap device
 jpeg JPEG bitmap device
 bmp BMP bitmap device
 tiff TIFF bitmap device

The function [Link]() displays the list of open devices:


x11(); x11(); pdf()
[Link]()

To know what is the active device:


[Link]()

to change the active device:


[Link](3)

To close a device:
[Link]()

R then displays the number of the new active device:


[Link](2)

Two specific features of the Windows version of R:


[Link] – To open with a function

4.1.2 Partitioning a graphic


To partitions the active graphical device
[Link](c(1, 2))

Divides the device into two parts: screen(1) or screen(2);

To deletes the last drawn graph


[Link]()

A part of the device can itself be divided with [Link]()

The function layout partitions the active graphic window in several parts where the graphs will
be displayed.

Its main argument is a matrix with integer numbers indicating the numbers of the “sub-
windows". For example, to divide the device into four equal parts:
layout(matrix(1:4, 2, 2))

It is of course possible to create this matrix previously allowing to better


visualize how the device is divided:
mat <- matrix(1:4, 2, 2)
mat
layout(mat)
[Link](4)

layout(matrix(1:6, 3, 2))
[Link](6)

layout(matrix(1:6, 2, 3))
[Link](6)

m <- matrix(c(1:3, 3), 2, 2)


layout(m)
[Link](3)

layout()  partitions the device with regular heights and widths:


this can be modified with the options widths and heights.

m <- matrix(1:4, 2, 2)
layout(m, widths=c(1, 3), heights=c(3, 1))
[Link](4)

m <- matrix(c(1,1,2,1),2,2)
m
layout(m, widths=c(2, 1), heights=c(1, 2))
[Link](2)

Graphical functions

x <- c(1:5)
y <- x
plot(x,y)
sunflowerplot(x,y)
pie(x)
boxplot(x)
stripchart(x)
coplot(x~y)
matplot(x,y)
dotchart(x)
[Link](x)
[Link](x)
hist(x)
barplot(x)
qqnorm(x)
qqplot(x,y)

par(pch=22, col="red")
par(mfrow=c(2,4)) # all plots on one page
opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(opts)){
heading = paste("type=",opts[i])
plot(x, y, type="n", main=heading, add=TRUE)
lines(x, y, type=opts[i])
}

dotchart(x)
abline(x,y)
text(x, y, expression(p == over(1, 1+e^-(beta*x+alpha))))
text(x,y,expression(p==over(a,a+b)))

x=rnorm(10)
y=rnorm(10)
plot(x,y)
plot(x, y, xlab="Ten random values", ylab="Ten other values",
xlim=c(-2, 2), ylim=c(-2, 2), pch=22, col="red",
bg="yellow", bty="l", tcl=0.4,
main="How to customize a plot with R", las=1, cex=1.5)

opar <- par()


par(bg="lightyellow", [Link]="blue", mar=c(4, 4, 2.5, 0.25))
plot(x, y, xlab="Ten random values", ylab="Ten other values",
xlim=c(-2, 2), ylim=c(-2, 2), pch=22, col="red", bg="yellow",
bty="l", tcl=-.25, las=1, cex=1.5)
title("How to customize a plot with R (bis)", [Link]=3, adj=1)
par(opar)

opar <- par()


par(bg="lightgray", mar=c(2.5, 1.5, 2.5, 0.25))
plot(x, y, type="n", xlab="", ylab="", xlim=c(-2, 2),
ylim=c(-2, 2), xaxt="n", yaxt="n")
rect(-3, -3, 3, 3, col="cornsilk")
points(x, y, pch=10, col="red", cex=2)
axis(side=1, c(-2, 0, 2), tcl=-0.2, labels=FALSE)
axis(side=2, -1:1, tcl=-0.2, labels=FALSE)
title("How to customize a plot with R (ter)",
[Link]=4, adj=1, [Link]=1)
mtext("Ten random values", side=1, line=1, at=1, cex=0.9, font=3)
mtext("Ten other values", line=0.5, at=-1.8, cex=0.9, font=3)
mtext(c(-2, 0, 2), side=1, las=1, at=c(-2, 0, 2), line=0.3,
col="blue", cex=0.9)
mtext(-1:1, side=2, las=1, at=-1:1, line=0.2, col="blue", cex=0.9)
par(opar)

setwd("E:/PhD/5Review/Worked_Example/Normalized")
w1<- [Link]("Indian Liver Patient Dataset_Train.csv",header=T)
head(w1)
hist(w1$Age)
hist(w1$Age,main="Distribution of w1", xlab="w1")
hist(w1$Age,breaks=12,xlim=c(0,10))
hist(w1$Age,breaks=12,xlim=c(-1,2))
hist(w1$Age,breaks=12,xlim=c(0,2))
hist(w1$Age,breaks=12,xlim=c(1,1.3))
hist(w1$Age,breaks=12,xlim=c(0.9,1.3))

hist(w1$Age,main='Leaf BioMass in High CO2 Environment',xlab='BioMass of


Leaves',ylim=c(0,16))
stripchart(w1$Age,add=TRUE,at=10.5)

boxplot(w1$Age,
main='Leaf BioMass in High CO2 Environment',
ylab='BioMass of Leaves', xlab='x axis')

boxplot(w1$Age,
main='Leaf BioMass in High CO2 Environment',
xlab='BioMass of Leaves',
horizontal=TRUE)

hist(w1$Age,main='Leaf BioMass in High CO2 Environment',xlab='BioMass of


Leaves',ylim=c(0,16))
boxplot(w1$Age,horizontal=TRUE,at=15.5,add=TRUE,axes=FALSE)

hist(w1$Age,main='Leaf BioMass in High CO2 Environment',xlab='BioMass of


Leaves',ylim=c(0,16))
boxplot(w1$Age,horizontal=TRUE,at=16,add=TRUE,axes=FALSE)
stripchart(w1$Age,add=TRUE,at=15)

boxplot(breaks=0, w1$Age~w1$SGPT)

plot(w1$Age,w1$SGPT)
cor(w1$Age,w1$SGPT)

plot(w1$Age,w1$SGPT,
main="Relationship Between Stem and Leaf Biomass",
xlab="Stem Biomass",
ylab="Leaf Biomass")
qqnorm(w1$Age)

qqnorm(w1$Age,
main="Normal Q-Q Plot of the Leaf Biomass",
xlab="Theoretical Quantiles of the Leaf Biomass",
ylab="Sample Quantiles of the Leaf Biomass")
qqline(w1$Age)

x <- rnorm(10,sd=5,mean=20)
y <- 2.5*x - 1.0 + rnorm(10,sd=9,mean=0)
x;y
plot(x,y,xlab="Independent",ylab="Dependent",main="Random Stuff")
x1 <- runif(8,15,25)
y1 <- 2.5*x1 - 1.0 + runif(8,-6,6)
points(x1,y1,col=2)
x2 <- runif(8,15,25)
y2 <- 2.5*x2 - 1.0 + runif(8,-6,6)
points(x2,y2,col=3,pch=2)
points(x1,y1,col=2,pch=3)
points(x2,y2,col=4,pch=5)
legend(14,70,c("Original","one","two"),col=c(1,2,4),pch=c(1,3,5))

You might also like