0% found this document useful (0 votes)
17 views5 pages

Data Visualization Techniques in R

The document describes various data visualization techniques in R including histograms, kernel density plots, box plots, scatter plots, bar plots and pie charts. It provides the code to generate these visualizations on a dataset containing student grades and demographic information. The code demonstrates how to customize the visualizations by adding labels, colors, legends and inserting counts or percentages. Grouping and stacking options are also illustrated for bar plots.

Uploaded by

Sunitha Kishore
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)
17 views5 pages

Data Visualization Techniques in R

The document describes various data visualization techniques in R including histograms, kernel density plots, box plots, scatter plots, bar plots and pie charts. It provides the code to generate these visualizations on a dataset containing student grades and demographic information. The code demonstrates how to customize the visualizations by adding labels, colors, legends and inserting counts or percentages. Grouping and stacking options are also illustrated for bar plots.

Uploaded by

Sunitha Kishore
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

Data Visualization

Data Visualization

Sr No Task Command
1 Histogram of gpa
> hist(grades$gpa)
[File: [Link]]
2 Histogram of gpa
hist(grades$gpa, xlab="gpa", ylab="Frequency", main = "Histogram of
Do proper labelling and Heading gpa" )

3 Histogram of gpa > hist(grades$gpa, xlab="gpa", ylab = "Frequency", main =


Fill red colour "Histogram of gpa", col = "red" )

4 Histogram of gpa with Normal Curve


> x<-grades$gpa
> h<-hist(x, breaks=10, col="red", xlab ="GPA", main="Histogram of
gpa with Normal Curve")
> xfit<-seq(min(x), max(x), length =40)
> yfit<-dnorm(xfit, mean=mean(x), sd=sd(x))
> yfit<-yfit*diff(h$mids[1:2]*length(x))
> lines(xfit, yfit, col="blue", lwd=2)

5 Kernel Density of gpa


> d<-density(grades$gpa)
> plot(d, main="Kernel Density of GPA")
> polygon(d, col="blue", border = "black")

6 Box Plot of gpa


> boxplot(grades$gpa)

7 Box Plot of gpa


> boxplot(grades$gpa, xlab="Box Plot", ylab="gpa", main = "Box Plot
Label and colour of gpa", col="red")

[Type text] Page 1


Data Visualization

8 Box Plot of gpa versus ethnicity > boxplot(gpa~ethnicity, data = grades, main = "GPA versus
Ethnicity", xlab="Ethnicity", ylab="GPA")

9 Box Plot of gpa versus ethnicity > boxplot(gpa~ethnicity, data = grades, main = "GPA versus
One colour red Ethnicity", xlab="Ethnicity", ylab="GPA", col= "red")

10 Box Plot of gpa versus ethnicity


> boxplot(gpa~ethnicity, data = grades, main = "GPA versus
Different colours [Link](5) Ethnicity", xlab="Ethnicity", ylab="GPA", col= [Link](5))

11 Box Plot of gpa versus ethnicity


> boxplot(gpa~ethnicity, data = grades, main = "GPA versus
Different colours [Link](5) Ethnicity", xlab="Ethnicity", ylab="GPA", col= [Link](5))

12 Scatter Plot of Final versus gpa


> plot(grades$gpa, grades$final)
13 Scatter Plot of Final versus gpa
> plot(grades$gpa, grades$final, xlab = "gpa", ylab = "final", main
Label, Heading and colour dots as blue color ="Scatter Plot Final vs GPA", col="blue")

14 Scatter Plot of Final versus gpa


> plot(grades$gpa, grades$final, xlab = "gpa", ylab = "final", main
Label, Heading, colour dots as blue colour ="Scatter Plot Final vs GPA", col="blue")
and add regression line in colour red
> abline(lm(grades$final~grades$gpa), col="red")

15 Quantile Comparison Chart through R


Commander
16 Scatter Plot though R Commander
17 Bag Plot (final vesus gpa)
> library(aplpack)

[Type text] Page 2


Data Visualization

Loading required package: tcltk


> attach(grades)
> bagplot(final, gpa, xlab="gpa", ylab ="final", main = "Bag Plot")

18 Scatter Plot Matrix through R


Commander
(final, gpa, quiz1)

19 Scatter Plot Matrix through R


Commander
(final, gpa, quiz1, quiz2, quiz3, quiz4, quiz5)
20 Bar Plot of ethnicity > grades <-
[Link]("C:/Users/inurture1/Desktop/Datafiles/[Link]")
Package barplot in available in base > View(grades)
packages > counts<-table(grades$ethnicity)
> barplot(counts, main = "Bar Plot of Etnicity", xlab="Etninicity",
ylab = "Counts", col = "red")

21 Make plot horizontal > barplot(counts, main = "Bar Plot of Etnicity", xlab="Etninicity",
horiz = T, ylab = "Counts", col = "red")

22 Label 1, 2, 3, 4 and 5 as Native, Asian, > barplot(counts, main = "Bar Plot of Etnicity", xlab="Etninicity",
ylab = "Counts", col = "red", [Link] = c("Native", "Asian",
Black, White & Hispanic respectively "Black", "White", "Hispanic"))

23 Different colours for each bar > barplot(counts, main = "Bar Plot of Etnicity", xlab="Etninicity",
ylab = "Counts", col = c("red", "blue", "green", "yellow",
"brown"), [Link] = c("Native", "Asian", "Black", "White",
"Hispanic"))

24 Insert Counts within bars > bp<-barplot(counts, main = "Bar Plot of Etnicity",
xlab="Etninicity", ylab = "Counts", col = c("red", "blue", "green",
"yellow", "brown"), [Link] = c("Native", "Asian", "Black",
"White", "Hispanic"))

[Type text] Page 3


Data Visualization

> text(bp,0,counts, cex=1,pos=3)

25 Insert Percentages within bars > [Link]<-counts/(sum(counts))*100


> bp<-barplot([Link], main = "Bar Plot of Etnicity",
xlab="Etninicity", ylab = "Counts", col = c("red", "blue", "green",
"yellow", "brown"), [Link] = c("Native", "Asian", "Black",
"White", "Hispanic"))
> text(bp,0,round([Link],1), cex =1, pos = 3)

26 Stacked Bar Chart ethnicity versus


> counts<-table(grades$gender, grades$ethnicity)
gender > barplot(counts, main = "Distribution of Ethnicity by Gender",
xlab="Ethnicity", col=c("blue", "red"), legend = rownames(counts),
[Link] = c("Native", "Asian", "Black", "White", "Hispanic"))

27 Stacked Bar Chart ethnicity versus


> barplot(counts, main = "Distribution of Ethnicity by Gender",
gender xlab="Ethnicity", col=c("blue", "red"), legend = c("Female",
(Give names Female to 1 and Male to 2) "Male"), [Link] = c("Native", "Asian", "Black", "White",
"Hispanic"))

28 Grouped Bar Chart ethnicity versus


> counts<-table(grades$gender, grades$ethnicity)
gender > barplot(counts, main = "Distribution of Ethnicity by Gender",
(Give names Female to 1 and Male to 2) xlab="Ethnicity", col=c("blue", "red"), legend = c("Female",
"Male"), [Link] = c("Native", "Asian", "Black", "White",
"Hispanic"), beside=T)

29 Pie Chart of Ethnicity


> vinod=c(5,20,24,45,11)
> names(vinod)=c("Native", "Asian", "Black", "White", "Hispanic")
> pie(vinod)

30 Vioplot of gpa
> library(vioplot)
Package vioplot is already available in base > vioplot(grades$gpa, col = "red")
packages > title ("Violin Plot of gpa", xlab = "", ylab = "gpa")

[Type text] Page 4


Data Visualization

31 > library(vioplot)
Violin Plot of gpa versus sections > x1<-grades$gpa[grades$section==1]
> x2<-grades$gpa[grades$section==2]
> x3<-grades$gpa[grades$section==3]
> vioplot(x1,x2,x3, names = c("Section 1", "Section 2", "Section
3"), col="gold")
> plot(1,1, xlim = c(0,3.5), ylim = range(c(x1,x2,x3)), type = "n",
xlab = "", ylab = "", axes = FALSE)
> axis(side = 1, at=1:3, labels=c("1st", "2nd","3rd"))
> axis(side=2)
> vioplot(x1,at=1, col="blue", add = TRUE)
> vioplot(x2,at=2, col="RED", add = TRUE)
> vioplot(x3,at=3, col="green", add = TRUE)
> title("Violin Plot of gpa versus Sections", xlab= "Sections",
ylab = "gpa")
> legend("topleft", fill = c("blue", "red", "green"), legend =
c("First", "Second", "Third"), [Link] = 1, cex=0.7)
32

[Type text] Page 5

You might also like