05 Charts and Graphs in R
05 Charts and Graphs in R
in R
Charts and Graphs supported
• Pie chart
• Bar chart
• Box plots
• Historams
• Line
graphs
• Scatter
plots
Pie charts
• R Programming language has numerous
libraries to create charts and graphs.
• A pie-chart is a representation of values as
slices of a circle with diff erent colors.
• The slices are labeled and the numbers
corresponding to each slice is also
represented in the chart.
• In R the pie chart is created using the pie()
funct ion which t akes positive numbers as a
vector input.
• The additional parameters are used to control
labels, color, title etc.
Pie charts – Syntax
• The basic synt ax f or creat ing a pie-chart using t he R is
−
pie(x, labels, radius, main, col, clockwise)
• Following is the description of the parameters used −
– x is a vector containing the numeric values used
in the pie chart.
– labels is used to give description to the slices.
– radius indicates the radius of the circle of the pie
chart.(value between −1 and +1).
– main indicates the title of the chart.
– col indicates the color palette.
– clockwise is a logical value indicating if the slices
are drawn clockwise or anti clockwise.
Pie charts – Example
# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore",
"Mumbai")
piepercent<- round(100*x/sum(x), 1)
png(file = "city_percentage_legends.png")
Values <-
matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11
),nrow =
3,ncol = 5,byrow = TRUE)
png(file = "barchart_stacked.png")
dev.off()
Bar chart – Stacked
Boxplot
• Boxplots are a measure of how well
distribut ed is the data in a data set.
• It divides the data set into three quartiles.
This graph represents the minimum,
maximum, median, fi rst quartile and third
quartile in the data set.
• It is also useful in comparing the
distribution of data across data sets by
drawing boxplots for each of them.
• Boxplots are created in R by using the
boxplot() function.
Boxplot – Syntax
• The basic syntax to create a boxplot in R is −
boxplot(x, data, notch, varwidth, names, main)
• Following is the description of the parameters used
−
– x is a vector or a f ormula
– data is the data frame.
– notch is a logical value. Set as TRUE to draw a
notch.
– varwidth is a logical value. Set as true to draw
width of the box proportionate to the sample
size.
– names are the group labels which will be
printed under each boxplot.
– main is used to give a title to the graph.
Boxplot – Example
• We use the data set "mtcars" available
in the R environment to create a basic
boxplot. Let's look at the columns
"mpg" and "cyl" in mtcars.
dev.off()
Histogram – Example
Line graph
• A line chart is a graph that connects a series of
points by drawing line segments between them.
• These points are ordered in one of their
coordinate (usually the x-coordinate) value.
• Line charts are usually used in identifying the
trends in data.
• The plot() function in R is used to create the line
graph.
Line graph – Syntax
• The basic synt ax t o create a line chart in R is −
plot(v,type,col,xlab,ylab)
• Following is the description of the parameters
used −
– v is a vector containing the numeric values.
– type takes the value "p" to draw only the
points, "i" to draw only the lines and "o" to
draw both points and lines.
– xlab is the label for x axis.
– ylab is the label for y axis.
– main is the Title of the chart.
– col is used to give colors to both the points
and lines.
Line graph – Example
# Create the data for the chart.
v <- c(7,12,28,3,41)
# Save the
file. dev.off()
Line graph – example.
Multiple lines in chart
# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
dev.off()
Multiple lines in chart
ScatterPlot
• Scatterplots show many points plotted
in the Cartesian plane.
• Each point represents t he values
of t wo variables.
• One variable is chosen in the horizontal
axis and another in the vertical axis.
• The simple scatterplot is created
using the plot() function.
ScatterPlot – Example
• We use the data set "mtcars" available
in the R environment t o creat e a basic
scat t erplot .
• Let's use the columns "wt" and "mpg"
in mtcars.
png(file = "scatterplot.png")
# Plot the chart for cars with weight between 2.5 to 5 and
mileage between 15 and 30.
plot(x = input$wt,y = input$mpg,
xlab = "Weight",
ylab = "Milage",
xlim = c(2.5,5),
ylim = c(15,30),
main = "Weight vs Milage"
)
dev.off()
ScatterPlot – Example
Scatter Plot matrices
• When we have more than two variables and we
want to find the correlation between one
variable versus the remaining ones we use
scatterplot matrix.
• We use pairs() function to create matrices of
scatterplots.
– Syntax:
pairs(formula, data)
– Following is the description of the
parameters used −
formula represents the series of variables used
in pairs.
data represents the data set from which the
variables will be taken.
Scatter Plot matrices – Example
# Give the chart file a name.
png(file = "scatterplot_matrices.png")
pairs(~wt+mpg+disp+cyl,data = mtcars,main =
"Scatterplot Matrix")
dev.off()
Scatter Plot matrices – Example
Regression
Useful resources
Thank you
This presentation is created using LibreOffice Impress 4.2.8.2, can be used freely as per GNU General Public
License