initializing device/ setting drawing parameters · web viewinitializing device/ setting drawing...
TRANSCRIPT
Initializing Device/ Setting Drawing Parameters
To create a new graph window –
> plot.new() // Or, > dev.new()
The call to plot.new chooses a default rectangular plotting region for the plot to appear in.The plotting region is surrounded by four margins as shown
To set margins for plotting area, we call to a function 'par()' before calling plot.new().
To set margins in inch
> par(mai=c(2, 2, 1, 1))
To set margins in lines of text
> par(mar=c(4, 4, 2, 2))
To set plot width and height
> par(pin=c(5, 4))
To set ranges for horizontal and vertical axes, after executing plot.new() function
> plot.window(xlim= c(-2*pi, 2* pi), ylim = c(-1, 1))
Above command produce axis limits which are expanded by 6% over those actually specified. This expansion can be inhibited by using 'xaxs' and 'yaxs' attributes and setting their values to "i" as
> plot.window(xlim = c(0, 1), ylim = c(10, 20), xaxs = "i", yaxs = "i")
To specify aspect ratio, i.e., fraction of change in 'y' corresponding to change in 'x', use 'asp' attribute, as
> plot.window(xlim = c(0, 1), ylim = c(10, 20), xaxs = "i", yaxs = "i", asp = 0.8)
To make active graph inactive
> X11()
To save to be created
> dev.new()
> png(file= "….")
> dev.off()
To close graph window once activated – Ans.
To set plot margins in inches – Ans.
To set difference parameters of newly creating graphics
First close all existing graphic windows. Then use 'par()' function.
To set background color of the new created graphics
Inside par() function use "bg= " attribute and set color.
To remove rectangular border around graphic window
Inside par() function set "bty=" attribute to "n"
To enlarge or reduce text size inside graphics
Inside par() function set "cex=" attribute to 0.25, 0.5, 0.75 etc for reducing of 1.2, 1.35, 1.5, etc for enlarging.
To display two or more graphs in the same graphic window
Inside par() function set "mfrow=" attribute to "c(1,2)" for two graphs, or "c(1, 2, 3)" for three graphs and so on.
Scatter Plots (Or, Dot plots, or XY-plot)
To create scatter plot of a sequence of numbers
> aa=c(23, 43, 34, 54, 35)
> plot(aa)
To create scatter plot of two sequences (of same length) one at horizontal and other at vertical axes.
> plot(1:10, 1:10)
> plot(c(1991,1992,1993,1994,1995), c(25, 54, 45, 66, 87))
Notes:
To display graph title
Inside plot() command use "main=" attribute
To display X axis title and Y axis titles
Inside plot() command use "xlab=" and "ylab=" attributes
To change color of indicators
Inside plot() command use "col=" attribute and set it to "red", "blue", etc.
To change pattern of scatter symbols
Inside plot() command use "pch=" and set it to 2(for triangle), 3 (for plus), 4 (for cross), etc.
The code of different available symbols are shown below-
By default circle is displayed as pattern.
To display lines to join dots in the plot
After creating a scatter plot, use "lines()" function. For exm.
> plot(c(1991,1992,1993,1994,1995), c(25, 54, 45, 66, 87))
> lines(c(1991,1992,1993,1994,1995), c(25, 54, 45, 66, 87))
To display horizontal line in the scatter plot at desired location
> abline(h=5)
To display vertical line in the scatter plot at desired location
> abline(v=1993)
To display line of given intercept and given slope
> abline(coef=c(35, 2)) // not doing.
To add extra points in an existing scatter plot
> points(1:5, c(4, 4, 4, 4, 4), pch=1:5, col=1:5) //not doing
To create scatter plot of data in a dataframe
> proteinconc = read.csv("proteinconc1.csv", header=TRUE)
> plot(proteinconc[,1])
> lines(proteinconc[ ,1])
To create new scatter plot on the top of existing one
> par(new = TRUE)
> plot(proteinconc[,2])
> lines(proteinconc[ ,2], col = "red")
To create legend for two scatter plots
> legend(x="topright", legend=c("Nucleus","Nuclear Membrane"), lwd=2, col = 1:2, bg="grey")
To create 2D scatter plot
The data file 'mtcars.csv' in included and displayed below
> dat = read.csv(file="mtcars.csv", header= T)
> dat
Now to create scatter plot for 'Cylinder' versus 'Mileage'
> plot(~Cylinder+Mileage, data=dat)
To create two separate scatter plots for Cylinder and Mileage in paired form as
> pairs(~Cylinder+Mileage, data=dat)
To create paired plots for Mileage, Cylinder and Weight, as
> plot(~Cylinder+Mileage+Weight, data = dat)
Or,
> pairs(~Cylinder+Mileage+Weight, data = dat)
Line Graph
To create line graph of following values in vector
> v = c(34, 25, 32, 35, 41, 42, 36, 33, 44, 22, 11, 5, 6, 15, 23)
Now to create line graph of these values use plot() function and then include attribute 'type =' and set its value to "o", as
> plot(v, type="o")
To display multiple lines in the graph, use lines() function, as discussed in 'Scatter Plot' section.
HistogramsThe basic syntax is
hist(v,main,xlab,xlim,ylim,breaks,col,border)
v is a vector containing numeric values used in histogram. main indicates title of the chart. col is used to set color of the bars. border is used to set border color of each bar. xlab is used to give description of x-axis. xlim is used to specify the range of values on the x-axis. ylim is used to specify the range of values on the y-axis. breaks is used to mention the width of each bar.
To create histogram for following values in vector
> v = c(34, 25, 32, 35, 41, 42, 36, 33, 44, 22, 11, 5, 6, 15, 23)
To create histogram of above values
> hist(v)
To fill color and display border of different color
> hist(v, xlab="Weight", col="Light Blue", border="Red")
To fix range of horizontal and vertical axes in histogram
> hist(v, xlab="Weight", col="Light Blue", border="Red", xlim = c(0,60), ylim=c(1,6))
To create histogram for data in dataframe-
The data frame file "proteinconc.xls" is available in working directory.
A csv version of this file is also created there. The data are as follows-
First we need to create a data frame for csv file by using
> proteinconc = read.csv("proteinconc1.csv", header = TRUE)
To create a histogram of 'nucleus' column
> hist(proteinconc[, 1])
To set column heading of respective column as title for histogram, we can use "main=" attribute as follows-
> hist(proteinconc[, 2], main = colnames(proteinconc[2])
To set number of rectangles in histogram to a fixed value
Use "breaks= " attribute in hist() function and set it to required number.
To set number and width of rectangles in histogram
Use "breaks= " attribute and use vector c(0.5, 0.7, 0.8, 0.9,1)
To plot frequency density in vertical axis of histogram instead of frequency
Set "freq=" to FALSE in hist() function.
BoxplotTo create boxplot of all numeric fields in "proteinconc.csv" file
> proteinconc = read.csv("proteinconc1.csv", header=TRUE)
> boxplot(proteinconc)
To create box plot of only values in second column of above file
> boxplot([, 2])
The dataset 'mtcars' stores information on number of cylinders of different models of cars and their mileage.
Now to create box plot for 'mileage' data
> mpg = Read.csv("mtcars.csv", header= TRUE)
> boxplot(mpg $ Mileage)
To create 4 different box plots for different "cyl" values, as
> boxplot(mpg $ Mileage ~ mpg $ Cylinder)
Similarly, box plots for different models can be displayed as
To provide different colors to above boxplots, as
> boxplot(mpg$Mileage ~ mpg$Model, col=c("Green","Yellow","Red","Brown","Blue"))
To display notched boxes instead of rectangle, as below
> boxplot(mpg$Mileage ~ mpg$Model, col=c("Green","Yellow","Red","Brown","Blue"), notch=TRUE)
The basic syntax of boxplot() function is as follows
boxplot(x, data, notch, varwidth, names, main)
x is a vector or a formula. data is the data frame. notch is a logical value. Set as TRUE to draw a notch. varwidth is a logical value. Set as true to draw width of the box proportionate to the sample size.
names are the group labels which will be printed under each boxplot. main is used to give a title to the graph.
Bar Graph
To create simple bar graph of any five values, say, 25, 54, 57, 65, 34
> barplot(c(25, 54, 57, 65, 34))
Or,
> gg= c(25, 54, 57, 65, 34)
> barplot(gg)
To provide labels, say, Jhapa, Kaski, Kavre, Patan, Ilam to above 5 bars in graph
> barplot(gg, names.arg=c("Jhapa", "Kaski", "Kavre", "Patan", "Ilam"))
To create bar graph from dataframe
Create a '.csv' of form as shown below and save it in name 'prod.csv'.
Load the data frame by
> prod1 = read.csv("prod.csv", header=TRUE)
Now to create bar graph of 'Production' column
> barplot(prod1[,2])
Next to create the bar graph with "year" as X axis label
> barplot(prod1[,2], names.arg=prod1[,1], col="Grey")
To display border of color (other than black) around bars
Use "border = " attribute in barplot() function.
To create stacked bar chart of form as shown below
First create a vector for three regions by
> reg= c("East", "West", "North")
Create a vector for month names by
> months=c("Mar","Apr","May","Jun","Jul")
Create a vector for three different colors for three different regions by
> colors=c("Green","Orange","Brown")
Next, create matrix for revenue values in the 3 regions as
> rev = matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow = 3,ncol = 5,byrow = TRUE)
Give the chart a name by
> png(file="ccc.png")
Create the bar plot by
> barplot(rev, main="Total Revenue", names.arg=months, xlab="Months", ylab="Revenue", col=colors)
Next, to provide legends to different regions
> legend(x="topleft", legend=reg, fill=colors)
Pie Chart To create pie chart with a vector of numbers
> dd = c(46, 75, 47, 65, 25, 57)
> pie(dd) // Or, pie(x = dd)
To create pie chart For following tabular text
City Beijing New Delhi Moscow Kathmandu MumbaiLength of road in km
1450 2500 1255 789 1435
> city = c("Beijing","New Delhi","Moscow", "Kathmandu", "Mumbai")
> road = c(145, 2500, 1255, 789, 1435)
> pie(city, road) // or, pie(x= road, labels = city)
To bring rainbow colors in the pie chart
> pie(x= road, labels = city, col = rainbow(length(road)))
To display values in pie chart and create legend for cities, as
> pie(x= road, labels = road, col = rainbow(length(road)))
> legend(x="topright", legend=city, fill=rainbow(length(road)), bg = "grey")
To display percentages in pie chart and create legend for cities, as
> percent = round(road/sum(road)*100,1)
> pie(x= road, labels = percent, col = rainbow(length(road)))
> legend(x="topright", legend=city, fill=rainbow(length(road)), bg = "grey")
To create 3D pie chart
Needs new package
??? From // www.tutorialspoint.com/r
And www.r-tutor.com