#Step_3
دو بردار را تعریف کرده
سپس با تابع
دامنه محور هامون رو از صفر تا ماکزیمم مقادیر اون بردارها در نظر گرفته ایم
با دستور پلات نمودار را رسم و با
روی محور x ها را به 5 قسمت مساوی تقسیم و از دوشنبه تا جمعه را به آنها اختصاص داده ایم
و بر روی محور y ها نیز همین کار را تکرار با این تفاوت که اندازه فاصله متوالی بین دو نقطه روی این محور دو برابر فاصله محور x میباشد
سپس جعبه ای برای این نمودار طراحی
و در ادامه دستور رسم نمودار
و بر روی بک گراند و محور ها برچسبی را طراحی و در اخر
راهنمای نمودار را داخل باکس قرار داده ایم که درباره ی این دستور بحث جداگانه ای را خواهیم داشت
@R_Experts
دو بردار را تعریف کرده
سپس با تابع
range( )
دامنه محور هامون رو از صفر تا ماکزیمم مقادیر اون بردارها در نظر گرفته ایم
با دستور پلات نمودار را رسم و با
axis( )
روی محور x ها را به 5 قسمت مساوی تقسیم و از دوشنبه تا جمعه را به آنها اختصاص داده ایم
و بر روی محور y ها نیز همین کار را تکرار با این تفاوت که اندازه فاصله متوالی بین دو نقطه روی این محور دو برابر فاصله محور x میباشد
سپس جعبه ای برای این نمودار طراحی
box( )
و در ادامه دستور رسم نمودار
lines ( )
و بر روی بک گراند و محور ها برچسبی را طراحی و در اخر
راهنمای نمودار را داخل باکس قرار داده ایم که درباره ی این دستور بحث جداگانه ای را خواهیم داشت
@R_Experts
#legend ( )
این دستور برای ایجاد راهنما یا برچسب در داخل نمودار ،
مورد استفاده قرار میگیرد که شامل آرگومان های مختلفی است که به ذکر چند مورد میپردازیم :
شروع این دستور با x,y خواهد بود که مختصات برچسب شما را
داخل یک باکس تعیین میکند که طول و عرض آن مستطیل حداکثر
در کجای محور مختصلت قرار گیرد .
در دستور
نوع برچسب یا راهنمای نمودار خود را نوشته
در
نوع رنگ را تعیین
و در
نوع شکل نقاط را مشخص میکنیم
اندازه فونت نوشته را مشخص
و
که رنگ باکس راهنما میباشد که به طور پیش فرض مشکی است میتواند تغیییر کند .
@R_Experts
legend(x, y = NULL, legend, fill = NULL, col = par("col"),
border = "black", lty, lwd, pch,
angle = 45, density = NULL, bty = "o", bg = par("bg"),
box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1,
adj = c(0, 0.5), text.width = NULL, text.col = par("col"),
text.font = NULL, merge = do.lines && has.pch, trace = FALSE,
plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,
inset = 0, xpd, title.col = text.col, title.adj = 0.5,
seg.len = 2)
این دستور برای ایجاد راهنما یا برچسب در داخل نمودار ،
مورد استفاده قرار میگیرد که شامل آرگومان های مختلفی است که به ذکر چند مورد میپردازیم :
شروع این دستور با x,y خواهد بود که مختصات برچسب شما را
داخل یک باکس تعیین میکند که طول و عرض آن مستطیل حداکثر
در کجای محور مختصلت قرار گیرد .
در دستور
fill
نوع برچسب یا راهنمای نمودار خود را نوشته
در
col
نوع رنگ را تعیین
و در
pch
نوع شکل نقاط را مشخص میکنیم
cex
اندازه فونت نوشته را مشخص
و
border
که رنگ باکس راهنما میباشد که به طور پیش فرض مشکی است میتواند تغیییر کند .
@R_Experts
#Example
@R_Experts
x2 <- c(4.1,1.1,-2.3,-0.2,-1.2,2.3)
y2 <- c(2.3,4.2,1.2,2.1,-2,4.3)
plot(x,y,cex=.8,pch=1,xlab="x",ylab="y",col="black")
points(x2,y2,cex=.8,pch=3,col="blue")
legend(x=-2,y=12,c("sample","control"),cex=.8,
col=c("black","blue"),pch=c(1,3))
@R_Experts
barplot(...)
funtion plot a bar chart. It's usage is:
barplot(height, width = 1, space = NULL,
names.arg = NULL, legend.text = NULL, beside = FALSE,
horiz = FALSE, density = NULL, angle = 45,
col = NULL, border = par("fg"),
main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
axes = TRUE, axisnames = TRUE,
cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
add = FALSE, args.legend = NULL, ...)
height
: Vector of each bar heights
width
: Vector of bar width
space
: Space between bars
col
: Vector of color for each bar
...
@R_Experts
#Example
#Step_1
First let's make a simple bar chart:
#Step_2
Let's add some annotations:
#Step_3
Suppose the bar chart above is about software department of our company, we are going to compare other department's revenues including hardware and services:
#Step_4
Let's compare the data sets horizontally:
@R_Experts
#Step_1
First let's make a simple bar chart:
>x <- c(3,2,6,8,4)
>barplot(x)
#Step_2
Let's add some annotations:
>barplot(x,border="tan2",names.arg=c("Jan","Feb","Mar","Apr","May"),
+ xlab="Month",ylab="Revenue",density=c(0,5,20,50,100))
#Step_3
Suppose the bar chart above is about software department of our company, we are going to compare other department's revenues including hardware and services:
>A <- matrix(c(3,5,7,1,9,4,6,5,2,12,2,1,7,6,8),nrow=3,ncol=5,byrow=TRUE)
>barplot(A,main="total revenue",names.arg=c("Jan","Feb","Mar","Apr","May"),
+ xlab="month",ylab="revenue",col=c("tan2","blue","darkslategray3"))
>legend(x=0.2,y=24,c("soft","hardware","service"),cex=.8,
+ col=c("tan2","blue","darkslategray3"),pch=c(22,0,0))
#Step_4
Let's compare the data sets horizontally:
>barplot(A,main="total revenue",beside=TRUE,
+ names.arg=c("Jan","Feb","Mar","Apr","May"),
+ xlab="month",ylab="revenue",col=c("tan2","blue","darkslategray3"))
>legend(x=1,y=11,c("soft","hardware","service"),cex=.8,
+ col=c("tan2","blue","darkslategray3"),pch=c(22,0,0))
@R_Experts
#Circle
function draws a circle on the plot. It's usage is:
: Circle center coordinates
: Circle radius
: Number of vertices
: Border Color
: Fill Color
: Line type
: Line width
draw.circle requires "
" package, to install:
for #Example
@R_Experts
draw.circle(...)
function draws a circle on the plot. It's usage is:
draw.circle(x,y,radius,nv=100,border=NULL,col=NA,lty=1,lwd=1)
x,y
: Circle center coordinates
radius
: Circle radius
nv
: Number of vertices
border
: Border Color
col
: Fill Color
lty
: Line type
lwd
: Line width
draw.circle requires "
plotrix
" package, to install:
>install.packages("plotrix")
for #Example
install.packages("plotrix")
plot(BOD)
require(plotrix)
draw.circle(4,14,2,border="blue",col="tan2")
@R_Experts
#Histogram
Is a popular descriptive statistical method that shows data by dividing the range of values into intervals and plotting the frequency/density per interval as a bar.
: value vector
: number of bars
...
for #Example
Plot a histogram:
@R_Experts
Is a popular descriptive statistical method that shows data by dividing the range of values into intervals and plotting the frequency/density per interval as a bar.
hist(x, breaks = "Sturges", freq = NULL, ...)
x
: value vector
breaks
: number of bars
...
for #Example
>x <- read.csv("histogram.csv",header=T,sep="\t")
>x <- t(x)
>ex <- as.numeric(x[2,1:ncol(x)])
Plot a histogram:
>hist(ex)
@R_Experts
The above plot is just a basic histogram. Let's add some parameters:
•br=20, #divide the data into 20 bars
•col="blue", #fill in blue color
•main="Histogram of Expression", #title of the histogram
•xlab="Expression", #x axis description
•ylab="Frequency", #y axis description
•freq=TRUE, #y axis is the frequency of each interval
Here is the command:
To add a density line into the histogram, we need to change two parameters:
•freq=FALSE, #y axis is the density value of each interval
•ylab="Density", #y axis description as Density
Here is the command:
We can write the plot into a file:
@R_Experts
•br=20, #divide the data into 20 bars
•col="blue", #fill in blue color
•main="Histogram of Expression", #title of the histogram
•xlab="Expression", #x axis description
•ylab="Frequency", #y axis description
•freq=TRUE, #y axis is the frequency of each interval
Here is the command:
>hist(ex,br=14,col="blue",xlab="Expression",ylab="Frequency",
+freq=TRUE,main="Histogram of Expression")
To add a density line into the histogram, we need to change two parameters:
•freq=FALSE, #y axis is the density value of each interval
•ylab="Density", #y axis description as Density
Here is the command:
>hist(ex,br=14,col="blue",xlab="Expression",ylab="Density",freq=FALSE,
+main="Histogram of Expression")
>lines(density(ex),col="red")
We can write the plot into a file:
>png("histogram3.png",400,300)
>hist(ex,br=14,col="blue",xlab="Expression",ylab="Density",
+freq=FALSE,main="Histogram of Expression")
>lines(density(ex),col="red")
>graphics.off()
@R_Experts