0% found this document useful (0 votes)
39 views6 pages

R Plots

The document describes various types of charts that can be created in R including pie charts, bar charts, box plots, histograms and 3D pie charts. It provides the syntax and examples of code for creating each type of chart in R and customizing aspects like labels, colors, titles and legends. Key functions discussed are pie(), barplot(), boxplot(), hist(), and pie3D(). Examples are given of how to create and customize each chart type as well as how to save the charts as image files.

Uploaded by

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

R Plots

The document describes various types of charts that can be created in R including pie charts, bar charts, box plots, histograms and 3D pie charts. It provides the syntax and examples of code for creating each type of chart in R and customizing aspects like labels, colors, titles and legends. Key functions discussed are pie(), barplot(), boxplot(), hist(), and pie3D(). Examples are given of how to create and customize each chart type as well as how to save the charts as image files.

Uploaded by

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

R Pie Charts

There is the following syntax of the pie() function:

1. pie(X, Labels, Radius, Main, Col, Clockwise)  
 X is a vector that contains the numeric values used in the pie chart.
 Labels are used to give the description to the slices.
 Radius describes the radius of the pie chart.
 Main describes the title of the chart.
 Col defines the color palette.
 Clockwise is a logical value that indicates the clockwise or anti-clockwise
direction in which slices are drawn.
# Creating data for the graph.  
x <- c(20, 65, 15, 50)  
labels <- c("India", "America", "Shri Lanka", "Nepal")  
# Giving the chart file a name.  
png(file = "Country.jpg")  
# Plotting the chart.  
pie(x,labels)  
# Saving the file.  
dev.off()  

pie(x,labels,main="Country Pie chart",col=rainbow(length(x)))  

Slice Percentage & Chart Legend

legend(x,y=NULL,legend,fill,col,bg)  

o x and y are the coordinates to be used to position the legend.


o legend is the text of legend
o fill is the color to use for filling the boxes beside the legend text.
o col defines the color of line and points besides the legend text.
o bg is the background color for the legend box.
# Creating data for the graph.  
x <- c(20, 65, 15, 50)  
labels <- c("India", "America", "Shri Lanka", "Nepal")  
pie_percent<- round(100*x/sum(x), 1)  
# Giving the chart file a name.  
png(file = "per_pie.jpg")  
# Plotting the chart.  
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)))  
#Saving the file.  
dev.off()  

3 Dimensional Pie Chart


# Getting the library.  
library(plotrix)  
# Creating data for the graph.  
x <- c(20, 65, 15, 50,45)  
labels <- c("India", "America", "Shri Lanka", "Nepal","Bhutan")  
# Give the chart file a name.  
png(file = "3d_pie_chart1.jpg")  
# Plot the chart.  
pie3D(x,labelslabels = labels,explode = 0.1, main = "Country Pie Chart")  
# Save the file.  
dev.off()  

R Bar Charts
barplot(h,x,y,main, names.arg,col)  

H A vector or matrix which contains numeric values used in the bar chart.
xlab A label for the x-axis.
ylab A label for the y-axis.
main A title of the bar chart.
names.arg A vector of names that appear under each bar.
col It is used to give colors to the bars in the graph.

# Creating the data for Bar chart  
H<- c(12,35,54,3,41)  
# Giving the chart file a name  
png(file = "bar_chart.png")  
# Plotting the bar chart   
barplot(H)  
# Saving the file  
dev.off()  

Labels, Title & Colors


# Creating the data for Bar chart  
H <- c(12,35,54,3,41)  
M<- c("Feb","Mar","Apr","May","Jun")  
  
# Giving the chart file a name  
png(file = "bar_properties.png")  
  
# Plotting the bar chart   
barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="Green",  
        main="Revenue Bar chart",border="red")  
# Saving the file  
dev.off()  

Group Bar Chart & Stacked Bar Chart


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)


# Giving the chart file a name

png(file = "bar_chart.png")

# Plotting the bar chart

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"))

# Saving the file

dev.off()

R Boxplot
boxplot(x, data, notch, varwidth, names, main)  

x It is a vector or a formula.

data It is the data frame.

notch It is a logical value set as true to draw a notch.

varwidth It is also a logical value set as true to draw the width of the box same as the sample size.

names It is the group of labels that will be printed under each boxplot.

main It is used to give a title to the graph.

# Giving a name to the chart file.  
png(file = "boxplot.png")  
# Plotting the chart.  
boxplot(mpg ~ cyl, data = mtcars, xlab = "Quantity of Cylinders",  
        ylab = "Miles Per Gallon", main = "R Boxplot Example")  
  
# Save the file.  
dev.off()  

R Histogram
hist(v,main,xlab,ylab,xlim,ylim,breaks,col,border)  

v It is a vector that contains numeric values.


main It indicates the title of the chart.
col It is used to set the color of the bars.
border It is used to set the border color of each bar.
xlab It is used to describe the x-axis.
ylab It is used to describe the y-axis.
xlim It is used to specify the range of values on the x-axis.
ylim It is used to specify the range of values on the y-axis.
breaks It is used to mention the width of each bar.

# Creating data for the graph.  
v <-  c(12,24,16,38,21,13,55,17,39,10,60)  
  
# Giving a name to the chart file.  
png(file = "histogram_chart.png")  
  
# Creating the histogram.  
hist(v,xlab = "Weight",ylab="Frequency",col = "green",border = "red")  
  
# Saving the file.  
dev.off()  

Using histogram return values for labels using text()

# Creating data for the graph.  
v <-  c(12,24,16,38,21,13,55,17,39,10,60,120,40,70,90)  
# Giving a name to the chart file.  
png(file = "histogram_return.png")  
  # Creating the histogram.  
m<hist(v,xlab = "Weight",ylab="Frequency",col = "darkmagenta",border = "pink", break
s = 5)  
#Setting labels  
text(m$mids,m$counts,labels=m$counts, adj=c(0.5, -0.5))  
# Saving the file.  
dev.off()  

You might also like