R(18), Chart
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-axisylab
: the label of y-axismain
: the title of the barnames
.arg: a vector of names of each barscol
: 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):
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 functiondata
: data framenotch
: 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 boxmain
: chart title
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 datamain
: title of chartcol
: colorsborder
: frame colors of each barxlab
: description of x-axisxlim
: range of values in x-axisylim
: range of values in y-axisbreak
: width of each bar
Line Chart
create line chart using function plot()
.
plot(v,type,col,xlab,ylab)
v
: vectortype
:="p"
, only plot="l"
, only line="o"
, plot and line
xlab
: label of x-axisylab
: label of y-axismain
: title of the chartcol
: color of dots and lines
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")
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 coordinatesy
: a dataset whose values are vertical coordinatesmain
: titlexlab
: label on the horizontal axisylab
: label on the vertical axisxlim
: limit of the value of xylim
: limit of the value of yaxes
: whether two axes are drawn on the drawing
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 chartlabels
: give the descriptionradius
: the radius of the circle(the value is between -1 and +1)main
: titlecol
: colorclockwise
: The logical value of whether the fragment is drawn clockwise or counterclockwise.