R(18), Chart

Published: by Creative Commons Licence

Give a Name & Save the File

give the chart file a name:

png(file="file_name.png")

save the file:

dev.off()

Bar Chart

create bar chart using function barplot()

R language can draw vertical and horizontal bars in bar graph, and each bar can be given a different color.

barplot(H, xlab, ylab, main, names.arg, col)
  • H: a vector or matrix containing the values used in a bar chart.
  • xlab: the label of x-axis
  • ylab: the label of y-axis
  • main: the title of the bar
  • names.arg: a vector of names of each bars
  • col: colors of each bar

If the the row number of H is greater than 1, than, the bar chart will be like this (stacked bar chart):

bar chart

Box Plot

Box plot is a good measure of data distribution in data set. It divides the dataset into three quartiles. This chart represents the minimum, maximum, median, first quartile, and third quartile in the dataset.

It can also be used to compare dada distribution between datasets by drawing box plots of each dataset.

create box plot using function boxplot():

boxplot(x, data, notch, varwidth, names, main)
  • x: vector or function
  • data: data frame
  • notch: a logical value(==TRUE, draw Notch)
  • varwidth: a logical value(==TRUE, draw the wid of the box proportional to the sample size)
  • names: group of labels printed below each box
  • main: chart title

box plot

Histogram

The histogram represents the frequency of the values of the variables stored in the range. A histogram is similar to a bar chart, but the difference is that values are grouped into continuous ranges. Each column in the histogram represents the height of the number of values present in the range.

create histogram using function hist()

hist(v,main,xlab,xlim,ylim,breaks,col,border)
  • v: vector of the data
  • main: title of chart
  • col: colors
  • border: frame colors of each bar
  • xlab: description of x-axis
  • xlim: range of values in x-axis
  • ylim: range of values in y-axis
  • break: width of each bar

histogram

Line Chart

create line chart using function plot().

plot(v,type,col,xlab,ylab)
  • v: vector
  • type:
    • ="p", only plot
    • ="l", only line
    • ="o", plot and line
  • xlab: label of x-axis
  • ylab: label of y-axis
  • main: title of the chart
  • col: color of dots and lines

line chart 1

We can use lines() function to draw several lines on the same graph: After the first line is drew, lines() can use an extra vector as input to draw the second line.

lines(t, type = "o", col = "blue")

line chart 2

Scatter Plot

The scatter graph shows many points drawn in the Cartesian plane. Each point represents the value of two variables. Select one variable on the horizontal axis and another on the vertical axis.

In the previous study, we can find that we can use to plot() to draw scatter plot.

plot(x, y, main, xlab, ylab, xlim, ylim, axes)
  • x: a dataset whose values are horizontal coordinates
  • y: a dataset whose values are vertical coordinates
  • main: title
  • xlab: label on the horizontal axis
  • ylab: label on the vertical axis
  • xlim: limit of the value of x
  • ylim: limit of the value of y
  • axes: whether two axes are drawn on the drawing

scatter plot

We use the function pairs() to create the matrix of the scatter plot.

pairs(formula, data, main)
  • formula: A series of variables used in pairs.
  • data: datasets we get variables from
  • main: title

Pie Chart

Pie charts are slices that represent values as circles of different colors, the slice is marked and the number corresponding to each slice is also shown in the chart.

We use function pie() to create pie chart in R.

pie(x, labels, radius, main, main, col, clockwise)
  • x: the vector used in pie chart
  • labels: give the description
  • radius: the radius of the circle(the value is between -1 and +1)
  • main: title
  • col: color
  • clockwise: The logical value of whether the fragment is drawn clockwise or counterclockwise.

pie chart