0% found this document useful (0 votes)
33 views21 pages

R - Charts and Graphs

Uploaded by

palakarora10107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views21 pages

R - Charts and Graphs

Uploaded by

palakarora10107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

R – Charts and

Graphs
R – graphs

• There are hundreds of charts and graphs present in R. For example,


bar plot, box plot, mosaic plot, dot chart, coplot, histogram, pie chart,
scatter graph, etc.
Types of R – Charts
• Bar Plot or Bar Chart
• Pie Diagram or Pie Chart
• Histogram
• Scatter Plot
• Box Plot
R – Line Graphs

• A line graph is a chart that is used to display information in the form of a series of data points.
• Syntax: plot(v, type, col, xlab, ylab)

Parameters:
• v: This parameter is a contains only the numeric values
• type: This parameter has the following value:
• “p” : This value is used to draw only the points.
• “l” : This value is used to draw only the lines.
• “o”: This value is used to draw both points and lines
• xlab: This parameter is the label for x axis in the chart.
• ylab: This parameter is the label for y axis in the chart.
• main: This parameter main is the title of the chart.
• col: This parameter is used to give colors to both the points and lines.
example
# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)
# Plot the line chart.
plot(v, type = "o")

# Create the data for the chart.


v <- c(17, 25, 38, 13, 41)

# Plot the bar chart.


plot(v, type = "o", col = "green",
xlab = "Month", ylab = "Article Written",
main = "Article Written chart")
• Basic Syntax of lines()
• lines(x, y, type = "l", col, lwd, lty, ...)
• x, y: Coordinates of the points through which the line will be drawn.
• type:Type of plot (usually "l" for lines, "b" for both lines and points).
• col: Line color.
• lwd: Line width.
• lty: Line type (solid, dashed, dotted, etc.).
Lines()
# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)
t <- c(22, 19, 36, 19, 23)
m <- c(25, 14, 16, 34, 29)

# Plot the bar chart.


plot(v, type = "o", col = "red",
xlab = "Month", ylab = "Article Written ",
main = "Article Written chart")

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


lines(m, type = "o", col = "green")
Bar Plot or Bar Chart
Bar plot or Bar Chart in R is used to represent the values in data vector as height of the
bars.
The data vector passed to the function is represented over y-axis of the graph.
Bar chart can behave like histogram by using table() function instead of data vector.

Syntax: barplot(data, xlab, ylab)

where:

•data is the data vector to be represented on y-axis


•xlab is the label given to x-axis
•ylab is the label given to y-axis
• # Create the data for the chart
• A <- c(17, 32, 8, 53, 1)

• # Plot the bar chart


• barplot(A, horiz = TRUE, xlab = "X-axis",
• ylab = "Y-axis", main ="Horizontal Bar Chart"
• )
Some properties
1.To add the title in bar chart.

barplot( A, main = title_name )

2. To add the label in bar chart.

barplot( A, xlab= x_label_name, ylab= y_label_name)

3.To add the color in bar chart.

barplot( A, col=color_name)
• # defining vector
• x <- c(7, 15, 23, 12, 28, 56, 64)

• # output to be present as PNG file


• png(file = "barplot.png")

• # plotting vector
• barplot(x, xlab = “ bar plot example",
• ylab = “ ", col = “orange",
• col.axis = “red",
• col.lab = “red")

• # saving the file


• dev.off()
Pie Diagram or Pie Chart
Pie chart is a circular chart divided into different segments according to the ratio of data provided.
The total value of the pie is 100 and the segments tell the fraction of the whole pie.
It is another method to represent statistical data in graphical form and pie() function is used to perform the same.
Syntax: pie(x, labels, col, main, radius)

• where,
• x is data vector
• labels shows names given to slices
• col fills the color in the slices as given parameter
• main shows title name of the pie chart
• radius indicates radius of the pie chart. It can be between -1 to +1
• # Create data for the graph.
• x<- c(23, 56, 20, 63)
• labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")

• # Plot the chart.


• pie(x, labels)
• Install.packages(“plotrix”)
• Library(plotrix)
• pie3D
Histogram
Histogram is a graphical representation used to create a graph with bars representing the frequency
of grouped data in vector. Histogram is same as bar chart but only difference between them is histogram
represents frequency of grouped data rather than data itself.

Syntax: hist(x, col, border, main, xlab, ylab)



where:
• x is data vector
• col specifies the color of the bars to be filled
• border specifies the color of border of bars
• main specifies the title name of histogram
• xlab specifies the x-axis label
• ylab specifies the y-axis label

• # defining vector
• x <- c(21, 23, 56, 90, 20, 7, 94, 12,
• 57, 76, 69, 45, 34, 32, 49, 55, 57)

• # output to be present as PNG file


• png(file = "hist.png")

• hist(x, main = "Histogram of Vector x",


• xlab = "Values",
• col.lab = "darkgreen",
• col.main = "darkgreen")

• # saving the file


• dev.off()
Scatter Plot
A Scatter plot is another type of graphical representation used to plot the points
to show relationship between two data vectors. One of the data vectors is represented
on x-axis and another on y-axis.

Syntax: plot(x, y, type, xlab, ylab, main)



Where,
• x is the data vector represented on x-axis
• y is the data vector represented on y-axis
• type specifies the type of plot to be drawn. For example, “l” for lines, “p” for points, “s” for stair steps, etc.
• xlab specifies the label for x-axis
• ylab specifies the label for y-axis
• main specifies the title name of the graph
• # taking input from dataset Orange already
• # present in R
• orange <- Orange[, c('age', 'circumference')]

• # output to be present as PNG file


• png(file = "plot.png")

• # plotting
• plot(x = orange$age, y = orange$circumference, xlab = "Age",
ylab = "Circumference", main = "Age VS Circumference",
col.lab = "darkgreen", col.main = "darkgreen",
col.axis = "darkgreen")

• # saving the file


• dev.off()
Box Plot

Box plot shows how the data is distributed in the data vector.
It represents five values in the graph i.e., minimum, first quartile, second quartile(median), third quartile,
the maximum value of the data vector.

Syntax: boxplot(x, xlab, ylab, notch)


where,

•x specifies the data vector


•xlab specifies the label for x-axis
•ylab specifies the label for y-axis
•notch, if TRUE then creates notch on both the sides of the box
• # defining vector with ages of employees
• x <- c(42, 21, 22, 24, 25, 30, 29, 22,
• 23, 23, 24, 28, 32, 45, 39, 40)

• # output to be present as PNG file


• png(file = "boxplot.png")

• # plotting
• boxplot(x, xlab = "Box Plot", ylab = "Age",
• col.axis = "darkgreen", col.lab = "darkgreen")

• # saving the file


• dev.off()
round() function is used to round numbers to
a specified number of decimal places
.
Basic Syntax of round()round(x, digits = n)
x: The number (or vector of numbers) you want to round.
digits: The number of decimal places to round to. If omitted, it defaults to 0 (rounds to the nearest integer).
Examples1. Round to the Nearest Integer: round(3.567) # Output: 42.
Round to a Specific Number of Decimal Places: round(3.567, digits = 2) # Output: 3.57
Other Rounding Functions1.
• ceiling(): Rounds up to the nearest integer.ceiling(3.2) # Output: 42.
• floor(): Rounds down to the nearest integer.floor(3.8) # Output: 33.
• trunc(): Truncates the decimal part, effectively rounding toward zero.trunc(3.8) # Output: 3
• signif(): Rounds to a specified number of significant digits.signif(3.567, digits = 2) # Output: 3.65.
• format(): Formats a number as text to control the number of decimal.
• format(3.567, nsmall = 2) # Output: "3.57“
These functions help in different rounding scenarios depending on the desired precision or rounding method.
legend()
• function is used to add a legend to a plot, which helps label different elements in
your visualization.
• You can customize its position, labels, colors, symbols, and more.
• Basic Syntax of legend() legend(x, y = NULL, legend, col, pch, lty, lwd, ...)
• x, y: Coordinates to position the legend. Alternatively, you can specify the
position as a keyword (e.g., "topright", "bottomleft")
• legend: A character vector of labels for the legend.
• col: Colors for the legend symbols.
• pch: Plotting characters (symbols) for the legend.
• lty: Line types for the legend (if lines are shown).
• lwd: Line widths for the legend.
• Other parameters like bg, cex, title, etc., allow further customization.
Example 1:
• # Sample data
• x <- 1:5
• y1 <- c(2, 4, 6, 8, 10)
• y2 <- c(1, 3, 5, 7, 9)
• # Plot points with different colors
• plot(x, y1, col = "blue", pch = 19, main = "Scatter Plot with Legend", xlab =
"X-axis", ylab = "Y-axis")
• points(x, y2, col = "red", pch = 17)
• # Add legend
• legend("topright", legend = c("Group 1", "Group 2"), col = c("blue", "red"),
pch = c(19, 17)

You might also like