INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
1. R Pie Chart
• R programming language has several libraries for creating charts and graphs.
• A pie-chart is a representation of values in the form of slices of a circle with different colors.
• Slices are labeled with a description, and the numbers corresponding to each slice are also shown in the chart.
• However, pie charts are not recommended in the R documentation, and their characteristics are limited.
• The authors recommend a bar or dot plot on a pie chart because people are able to measure length more accurately
than volume.
• The Pie charts are created with the help of pie () function, which takes positive numbers as vector input. Additional
parameters are used to control labels, colors, titles, etc.
• There is the following syntax of the pie() function:
pie(X, Labels, Radius, Main, Col, Clockwise)
1. X is a vector that contains the numeric values used in the pie chart.
2. Labels are used to give the description to the slices.
3. Radius describes the radius of the pie chart.
4. Main describes the title of the chart.
5. Col defines the color palette.
6. Clockwise is a logical value that indicates the clockwise or anti-clockwise direction in which slices are
drawn.
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
1. R Pie Chart
• Example
x <- c(20, 65, 15, 50)
labels <- c("India", "America", "Shri Lanka", "Nepal")
pie_percent<- round(100*x/sum(x), 1)
pie(x, labels = pie_percent, main = "Country Pie Chart",col = rainbow(length(x)))
legend("topright", c("India", "America", "Shri Lanka", "Nepal"), cex = 0.8, fill = rainbow(length(x)))
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
2. R Bar Charts
• A bar chart is a pictorial representation in which numerical values of variables are represented by length or height
of lines or rectangles of equal width.
• A bar chart is used for summarizing a set of categorical data. In bar chart, the data is shown through rectangular
bars having the length of the bar proportional to the value of the variable.
• In R, we can create a bar chart to visualize the data in an efficient manner. For this purpose,
• R provides the barplot() function, which has the following syntax:
barplot(h,x,y,main, names.arg,col)
S.No Parameter Description
1. H A vector or matrix which contains numeric values used in the bar chart.
2. xlab A label for the x-axis.
3. ylab A label for the y-axis.
4. main A title of the bar chart.
5. names.arg A vector of names that appear under each bar.
6. col It is used to give colors to the bars in the graph.
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
2. R Bar Charts
• Example
library(RColorBrewer)
months <- c("Jan","Feb","Mar","Apr","May")
regions <- c("West","North","South")
Values <- matrix(c(21,32,33,14,95,46,67,78,39,11,22,23,94,15,16), nrow = 3, ncol = 5, byrow = TRUE)
barplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue",
ccol=c("cadetblue3","deeppink2","goldenrod1"))
legend("topleft", regions, cex = 1.3, fill = c("cadetblue3","deeppink2","goldenrod1"))
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
3. R Histogram
• A histogram is a type of bar chart which shows the frequency of the number of values which are compared with a
set of values ranges.
• The histogram is used for the distribution, whereas a bar chart is used for comparing different entities.
• In the histogram, each bar represents the height of the number of values present in the given range.
• For creating a histogram, R provides hist() function, which takes a vector as an input and uses more parameters to
add more functionality.
• There is the following syntax of hist() function:
hist(v,main,xlab,ylab,xlim,ylim,breaks,col,border)
S.No Parameter Description
1. v It is a vector that contains numeric values.
2. main It indicates the title of the chart.
3. col It is used to set the color of the bars.
4. border It is used to set the border color of each bar.
5. xlab It is used to describe the x-axis.
6. ylab It is used to describe the y-axis.
7. xlim It is used to specify the range of values on the x-axis.
8. ylim It is used to specify the range of values on the y-axis.
9. breaks It is used to mention the width of each bar.
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
3. R Histogram
Example
v <- c(12,24,16,38,21,13,55,17,39,10,60)
hist(v,xlab = "Weight",ylab="Frequency",col = "green",border = "red")
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
4.R Scatterplots
• The scatter plots are used to compare variables. A comparison between variables is required when we need to
define how much one variable is affected by another variable.
• In a scatterplot, the data is represented as a collection of points. Each point on the scatterplot defines the values of
the two variables.
• One variable is selected for the vertical axis and other for the horizontal axis. In R, there are two ways of creating
scatterplot, i.e., using plot() function and using the ggplot2 package's functions.
• There is the following syntax for creating scatterplot in R:
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
S.No Parameters Description
1. x It is the dataset whose values are the horizontal coordinates.
2. y It is the dataset whose values are the vertical coordinates.
3. main It is the title of the graph.
4. xlab It is the label on the horizontal axis.
5. ylab It is the label on the vertical axis.
6. xlim It is the limits of the x values which is used for plotting.
7. ylim It is the limits of the values of y, which is used for plotting.
8. axes It indicates whether both axes should be drawn on the plot.
INTRODUCTION OF THE R LANGUAGE
Basic Plotting:
1. R Scatterplots
• Example
data <-mtcars[,c('wt','mpg')]
png(file = "scatterplot.png")
plot(x = data$wt,y = data$mpg, xlab = "Weight", ylab = "Milage", xlim = c(2.5,5), ylim = c(15,30), main = "Weig
ht v/sMilage")
dev.off()