Plot

Line plot

plot(x, y, main, xlab, ylab, xlim, ylim, axes)

  • "p" - points
  • "l" - lines
  • "b" - both points and lines
  • "c" - empty points joined by lines
  • "o" - overplotted points and lines
  • "s" and "S" - stair steps
  • "h" - histogram-like vertical lines
  • "n" - does not produce any points or lines
In [74]:
#png(file = "scatterplot.png") # output file name

# plot a scatter plot
plot(x = x <- seq(-pi,pi,0.1),y = sin(x),
   xlab = "Weight",
   ylab = "Milage",
   xlim = c(-3.5,3.5),
   ylim = c(-1,1),
   main = "Weight vs Milage",
   type = "o",
   col = "blue",
)

# add an overlap plot
points(x,cos(x), col="red")
lines(x, cos(x), col='red')

# add legend
legend("topleft",
c("sin(x)","cos(x)"),
fill=c("blue","red")
)

# output plot to the file
# dev.off()

Line plot

plot(v,type,col,xlab,ylab)

In [77]:
plot(x = x <- seq(-pi,pi,0.1),y = sin(x),
   xlim = c(-3.5,3.5),
   ylim = c(-1,1),
   type = "p",
   col = "blue"
)

Pie chart

pie(x, labels, radius, main, col, clockwise)

In [29]:
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

pie(x,labels, radius = 0.8, main = "Pie Chart", col = c("blue", "red", "yellow", "white"), clockwise=TRUE)

Bar Chart

barplot(H,xlab,ylab,main, names.arg,col)

In [31]:
H <- c(7,12,28,3,41)
barplot(H, xlab='month', ylab='production', main='bar chart', names.arg=c('Jan', 'Feb', 'Mar', 'Apr', 'May'), col='red')
In [42]:
colors = c("red","blue","yellow")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")

Values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11), nrow = 3, ncol = 5, byrow = TRUE)

barplot(Values, main = "total revenue", names.arg = months, xlab = "month", ylab = "revenue", col = colors)
legend("topleft", regions, cex = 1.3, fill = colors)

Boxplot

boxplot(x, data, notch, varwidth, names, main)

In [59]:
a = c(5, 7, 10, 15, 19, 21, 21, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24, 24, 25)
boxplot(a)

Histogram

hist(v,main,xlab,xlim,ylim,breaks,col,border)

In [72]:
v <-  c(9,13,21,8,36,22,12,41,31,33,19)
hist(v,xlab = "Weight", xlim = c(0,40), col = "yellow",border = "blue", breaks = 10)