|R| Experts
1.07K subscribers
375 photos
35 videos
58 files
204 links
@R_Experts
🔴آمار علم جان بخشیدن به داده‌هاست.
🔷ارتباط با ما
@iamrezaei
لینک یوتیوب و اینستاگرام و ویرگول:
https://zil.ink/expertstv
Download Telegram
 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:

>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



 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.

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:

>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
#Boxplot
usually refers to box-and-whisker plot, which is a popular method to show data by drawing a box around the 1st and 3rd quartile, and the whiskers for the smallest and largest data values, the median is represented by a bold line in the box.

Following is a csv file example "boxplot.csv", we will draw a boxplot of "Expression" based on Subtype "A", "B" and "C".

Let first read in the data from the file:

> x <- read.csv("boxplot.csv",header=T,sep="\t")
> x <- t(x)
> a <- as.numeric(x[2,1:143])
> b <- as.numeric(x[2,144:218])
> c <- as.numeric(x[2,219:ncol(x)])


Box plot based on subtype A, B and C:

> boxplot(a,b,c,col=c("red","blue","green"),names=c("A","B","C"),
+ xlab="Subtype",ylab="Expression")


The above plot shows that the Expression values for Subtype A, B and C are similar, however the two sub-boxes around the median of Subtype C is wider than B and A, the data are not symmetrically distributed around the median.

if the 'notch' parameter is 'TRUE', a notch is drawn in each side of the boxes. If the notches of two plots do not overlap this is 'strong evidence' that the two medians differ.

> boxplot(a,b,c,col=c("red","blue","green"),names=c("A","B","C"), 
+ notch=TRUE, xlab="Subtype",ylab="Expression")


We can write the plot into a file:

> png("boxplot1.png",400,300)
> boxplot(a,b,c,col=c("red","blue","green"),names=c("A","B","C"),
+ xlab="Subtype",ylab="Expression")
> graphics.off()


@R_Experts