Data Analytics with R





Line Charts
Overview
Line charts are created with the function lines(x, y, type=) where x and y are numeric vectors of (x,y) points to connect. type= can take the following values:
type
description
p
points
l
lines
o
overplotted points and lines
b, c
points (empty if "c") joined by lines
s, S
stair steps
h
histogram-like vertical lines
n
does not produce any points or lines
The lines( ) function adds information to a graph. It can not produce a graph on its own. Usually it follows a plot(x, y) command that produces a graph.
By default, plot( ) plots the (x,y) points. Use the type="n" option in the plot( ) command, to create the graph with axes, titles, etc., but without plotting the points.
Example
In the following code each of the type= options is applied to the same dataset. The plot( ) command sets up the graph, but does not plot the points.
x <- c(1:5); y <- x # create some data
par(pch=22, col="red") # plotting symbol and color
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)
  lines(x, y, type=opts[i])
}
lines options without points
Next, we demonstrate each of the type= options when plot( ) sets up the graph and does plot the points.
x <- c(1:5); y <- x # create some data
par(pch=22, col="blue") # plotting symbol and color
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, main=heading)
  lines(x, y, type=opts[i])
}
lines options with points
As you can see, the type="c" option only looks different from the type="b" option if the plotting of points is suppressed in the plot( ) command.
To demonstrate the creation of a more complex line chart, let's plot the growth of 5 orange trees over time. Each tree will have its own distinctive line. The data come from the dataset Orange.
# Create Line Chart

# convert factor to numeric for convenience
Orange$Tree<- as.numeric(Orange$Tree)
ntrees<- max(Orange$Tree)

# get the range for the x and y axis
xrange<- range(Orange$age)
yrange<- range(Orange$circumference)

# set up the plot
plot(xrange, yrange, type="n", xlab="Age (days)",
   ylab="Circumference (mm)" )
colors <- rainbow(ntrees)
linetype<- c(1:ntrees)
plotchar<- seq(18,18+ntrees,1)

# add lines
for (i in 1:ntrees) {
  tree <- subset(Orange, Tree==i)
  lines(tree$age, tree$circumference, type="b", lwd=1.5,
    lty=linetype[i], col=colors[i], pch=plotchar[i])
}

# add a title and subtitle
title("Tree Growth", "example of line plot")

# add a legend
legend(xrange[1], yrange[2], 1:ntrees, cex=0.8, col=colors,
   pch=plotchar, lty=linetype, title="Tree")
line chart

R Programming Fonts Exercise



# Type family examples - creating new mappings
plot(1:10,1:10,type="n")
windowsFonts(
  A=windowsFont("Arial Black"),
  B=windowsFont("Bookman Old Style"),
  C=windowsFont("Comic Sans MS"),
  D=windowsFont("Symbol")
)
Create above display code in R and give the output of the following.
1) text(3,3,"Hello World Default")
2) text(4,4,family="A","Hello World from Arial Black")
3) text(5,5,family="B","Hello World from Bookman Old Style")
4) text(6,6,family="C","Hello World from Comic Sans MS")
5) text(7,7,family="D", "Hello World from Symbol")
6) Find out 2 more windows font and give the output of same.

R programming


v <- c(25,62,12,34,55) pie(v) pie(v,col=rainbow(12)) pie(v,col=rainbow(12)) barplot(v) barplot(v,col=rainbow(12)) v <- c(25,62,12,34,55,12,3,42,15,30,45) barplot(v,col=rainbow(12)) v <- c(25,62,12,34,55,12,3,42,15,30,45,24,49,78) barplot(v,col=rainbow(12)) barplot(v,col=heat.colors(12)) barplot(v,col=terrain.colors(12)) barplot(v,col=topo.colors(12)) plot(1:10,1:10,type="n") plot(1:10,1:10,type="n") windowsFonts( A=windowsFont("Arial Black"), B=windowsFont("Bookman Old Style"), C=windowsFont("Comic Sans MS"), D=windowsFont("Symbol") ) plot(1:10,1:10,type="n") windowsFonts( A=windowsFont("Arial Black"), B=windowsFont("Bookman Old Style"), C=windowsFont("Comic Sans MS"), D=windowsFont("Symbol") ) text(3,3,"Hello World Default") plot(1:10,1:10,type="n") windowsFonts( A=windowsFont("Arial Black"), B=windowsFont("Bookman Old Style"), C=windowsFont("Comic Sans MS"), D=windowsFont("Symbol") ) text(3,3,"Hello World Default") text(4,4,family="A","Hello World from Arial Black") v v barplot(v) barplot(v,col=rainbow(10)) barplot(v,col=heat.colors(10)) barplot(v,col=terrrain.colors(10)) barplot(v,col=terrain.colors(10)) barplot(v,col=terrain.colors(1)) barplot(v,col=terrain.colors(4)) barplot(v,col=terrain.colors(5)) barplot(v,col=terrain.colors(6)) barplot(v,col=terrain.colors(12)) barplot(v,col=cm.colors(12)) input <- mtcars[,c("wt","mpg")] input input <- head(input) input plot (x=input$wt,y=input$mpg) plot (x=input$wt,y=input$mpg,xlab="weight",ylab="mpg") plot (x=input$wt,y=input$mpg,xlab="weight",ylab="mpg",pch=4) plot (x=input$wt,y=input$mpg,xlab="weight",ylab="mpg",pch=4,cex=2) plot (x=input$wt,y=input$mpg,xlab="weight",ylab="mpg",pch=4,cex=2,xlim=c(2,5)) mtcars women cars input plot (x=input$wt,y=input$mpg) plot (x=input$wt,y=input$mpg) input <- mtcars[,c("wt","mpg","dist","speed")] mtcars input <- mtcars[,c("wt","mpg","disp","cyl")] input <- head(input) input pairs(~wt+mpg+cyl+disp,data=mtcars) input <- mtcars[,c("wt","mpg","disp","cyl")] input <- head(input) input pairs(~wt+mpg+cyl+disp,data=input) input <- mtcars[,c("wt","mpg","disp")] input <- head(input) pairs(~wt+mpg+cyl+disp,data=input) values <- c(906, 264, 289, 339, 938) values pie(values) pie(values,labels=c("Rajkot","Ahmedabad","Vadodara,"Surat","Bhavanagar"")) pie(values,labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar")) per <- round(100*values/sum(values),1) per pie(values,labels=per) 100*values 100*values/sum(values) round(100*values/sum(values),1) per <- round(100*values/sum(values),1) pie(values,labels=per) pie(values,labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="red") pie(values,labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="red",lty=2) pie(values,labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border=c("red","green","blue","magenta"),lty=2) pie(values,labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border=rainbow(5),lty=2) pie(values,labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border=rainbow(5),lty=2) pie(values,col=rainbow(5),labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border=rainbow(5),lty=2) pie(values,col=rainbow(5),labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border=rainbow(5),lty=10) pie(values,col=rainbow(5),labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=10) pie(values,col=rainbow(5),labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) pie(values,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) pie(values,clockwise=TRUE,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) pie(values,clockwise=FALSE,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) pie(values,clockwise=TRUE,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) legend("topright",c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),fill=rainbow(5)) legend("topright",cex=1,c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),fill=rainbow(5)) legend("topright",cex=2,c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),fill=rainbow(5)) legend("topright",cex=0.5,c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),fill=rainbow(5)) pie(values,clockwise=TRUE,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) legend("topright",cex=0.5,c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),fill=rainbow(5)) pie(values,clockwise=TRUE,density=50,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),border="black",lty=40) pie(values,clockwise=TRUE,density=50,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),radius=1,border="black",lty=40) pie(values,clockwise=TRUE,density=50,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),radius=2,border="black",lty=40) pie(values,clockwise=TRUE,density=50,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),radius=1.5,border="black",lty=40) pie(values,clockwise=TRUE,density=50,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),radius=1.3,border="black",lty=40) pie(values,clockwise=TRUE,density=50,col=rainbow(5),main="Sample pie chart",labels=c("Rajkot","Ahmedabad","Vadodara","Surat","Bhavanagar"),radius=1,border="black",lty=40) any(grepl("plotrix"),installed.packages()) any(grepl("plotrix",installed.packages()) ) ) library("plotrix") pie3D(values) pie3D(values,explode=0.1) pie3D(values,explode=0.5) savehistory("D:/atmiya/subject/R/Command History/A_Command_History_08_03_2017.Rhistory")

Sales_order


Order_no Product_no Qty_ordered Qty_disp
O001 P001 4 4
O001 P002 3 2
O001 P003 2 2
O002 P004 3 3
O002 P001 3 3
O003 P003 3 2
O004 P004 4 3
O004 P001 5 3
Rainfall

Month Tokyo NewYork London Berlin
1 Jan 49.9 83.6 48.9 42.4
2 Feb 71.5 78.8 38.8 33.2
3 Mar 106.4 98.5 39.3 34.5
4 Apr 129.2 93.4 42.4 39.7
5 May 144 106 47 52.6
6 Jun 176 84.5 48.3 70.5
product master
--------


Product_no Product_name Profit_percent Qty_on_hand Sell_price Cost_price
P001 Mouse 5 100 525 500
P002 Monitor 6 20 12000 11200
P003 CD 5 30 1050 500
P004 DVD 5 200 50 40
P005 HDD 7 100 8400 8000
P006 Pendrive 4 90 500 450

1) Create a vector (4,6,3,4,6,3,…..,4,6,3) Where there are 10 occurrences of 4 Using repeat function ANS=> > v1 <- c(4,6,3,4,6,3,4,6,3) > v1 > rep(v1,time=2) [1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 > -------------------------------------------------------------- 2) Use the function paste to create the following character vectors of length 30: ("fn1", "fn2", ..., "fn30"). In this case, there is no space between fn and the number following. ANS=> > paste("fn",v3,sep = "") [1] "fn1" "fn2" "fn3" "fn4" "fn5" "fn6" "fn7" "fn8" "fn9" "fn10" "fn11" "fn12" [13] "fn13" "fn14" "fn15" "fn16" "fn17" "fn18" "fn19" "fn20" "fn21" "fn22" "fn23" "fn24" [25] "fn25" "fn26" "fn27" "fn28" "fn29" "fn30" > > paste(collapse ="fn",v3) [1] "1fn2fn3fn4fn5fn6fn7fn8fn9fn10fn11fn12fn13fn14fn15fn16fn17fn18fn19fn20fn21fn22fn23fn24fn25fn26fn27fn28fn29fn30" 4) Create a character vector like "Android", "Java", "R" , "C" , "PHP" and print length of each vector element output is like 7 4 1 1 3,. ANS=> > v4 <- c("Android", "Java", "R" , "C" , "PHP") > v4 > v4 [1] "Android" "Java" "R" "C" "PHP" > nchar(v4) [1] 7 4 1 1 3 > 5) create the function to do the sum of inputted number Example: no=235 then the output will be 2+3+5 = 10. ANS=> > sum(2+3+5) [1] 10 3) Matrix A = 1 1 3 5 2 6 -2 -1 -3 Q.3 Read “Rainfall.csv” and generate following charts. 1) Generate scatter plot of rainfall for Tokyo city with red color and double line thickness. Provide lines for New York ,London and Berlin city on the same plot with three different colors. 2) Generate a Barchart of matrix of rainfall for all cities Provide colors to bars and month name as legend ANS=>

Unleashing B2B Success: Mastering Event and Conversion Tracking with GA4

Tracking events and conversions is vital for B2B businesses using GA4 (Google Analytics 4) to optimize their marketing efforts. Here is a re...