Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Data Visualization

The document provides an overview of data visualization techniques in R, focusing on the default mtcars dataset. It covers various plotting functions including scatter plots, bar plots, histograms, boxplots, strip charts, and jitter plots, along with examples and parameters for customization. Additionally, it demonstrates how to save plots as image files and includes the use of libraries such as RColorBrewer and vioplot.

Uploaded by

k73733624
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)
6 views5 pages

Data Visualization

The document provides an overview of data visualization techniques in R, focusing on the default mtcars dataset. It covers various plotting functions including scatter plots, bar plots, histograms, boxplots, strip charts, and jitter plots, along with examples and parameters for customization. Additionally, it demonstrates how to save plots as image files and includes the use of libraries such as RColorBrewer and vioplot.

Uploaded by

k73733624
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/ 5

Data Visualization

default mtcars dataset for data visualization in R.

#To load graphics package


library("graphics")
#To load datasets package
library("datasets")
#To load mtcars dataset
data(mtcars)
#To analyze the structure of the dataset
str(mtcars)

The plot() Function


#To plot mpg(Miles per Gallon) vs Number of cars
plot(mtcars$mpg, xlab = "Number of cars", ylab = "Miles per Gallon", col =
"red")
plot(mtcars$hp,mtcars$mpg, xlab = "HorsePower", ylab = "Miles per Gallon",
type = "h", col = "blue")

Bar Plot in R
In R, we use the barplot() function to create bar plots. It is used to represent
data in the form of rectangular bars, both in vertical and horizontal ways, and
the length of the bar is proportional to the value of the variable.

Bar Plots is one of the most efficient ways of representing datas. It can be
used to summarize large data in visual form.
For example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)


# bar plot of temperatures vector
result <- barplot(temperatures)
print(result)

result <- barplot(temperatures,


main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),
col = "blue",
density = 20
)
print(result)

result <- barplot(temperatures,


main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),
col = "blue",
density = 20,
horiz = TRUE
)

# 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()

# 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()

max.temp <- c(22, 27, 26, 24, 23, 26, 28)


barplot(max.temp)
# barchart with added parameters
barplot(max.temp,
main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),
col = "darkred",
horiz = TRUE)

library(RColorBrewer)
months <- c("Jan","Feb","Mar","Apr","May")
regions <- c("West","North","South")
# Creating the matrix of the values.
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 = "stacked_chart.png")
# Creating the bar chart
barplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month",
ylab = "Revenue", ccol =c("cadetblue3","deeppink2","goldenrod1"))
# Adding the legend to the chart
legend("topleft", regions, cex = 1.3, fill =
c("cadetblue3","deeppink2","goldenrod1"))

# Saving the file


dev.off()

R Histogram

A histogram is a graphical display of data using bars of different heights.


Histogram is used to summarize discrete or continuous data that are
measured on an interval scale.

temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )
# histogram of temperatures vector
result <- hist(temperatures)
print(result)

# histogram with added parameters


hist(Temperature,
main="Maximum daily temperature at La Guardia Airport",
xlab="Temperature in degrees Fahrenheit",
xlim=c(50,100),
col="darkmagenta",
freq=FALSE
)

# 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")
temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )
# histogram of temperatures vector
result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit",
col = "red",
xlim = c(50,100),
ylim = c(0, 5))

print(result)

Boxplot

A boxplot is a graph that gives us a good indication of how the values in the
data are spread out. It is used to represent descriptive statistics of each
variable in a dataset. It represents the minimum, first quartile, median, third
quartile, and the maximum values of a variable.

# add title, label, new color to boxplot


boxplot(mtcars$mpg, main="Mileage Data Boxplot", ylab="Miles Per
Gallon(mpg)", xlab="No. of Cylinders", col="orange")

# Giving a name to our chart.


png(file = "boxplot_using_notch.png")
# Plotting the chart.
boxplot(mpg ~ cyl, data = mtcars,
xlab = "Quantity of Cylinders",
ylab = "Miles Per Gallon",
main = "Boxplot Example",
notch = TRUE,
varwidth = TRUE,
ccol = c("green","yellow","red"),
names = c("High","Medium","Low")
)

# Loading the vioplot package


library(vioplot)
# Giving a name to our chart.
png(file = "vioplot.png")
#Creating data for vioplot function
x1 <- mtcars$mpg[mtcars$cyl==4]
x2 <- mtcars$mpg[mtcars$cyl==6]
x3 <- mtcars$mpg[mtcars$cyl==8]
#Creating vioplot function
vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"),
col="green")
#Setting title
title("Violin plot example")

Strip chart

A strip chart can be used to visualize dozens of time series at once. Built-in
dataset named airquality to create a strip chart.
# use head() to load first six rows of airquality dataset
head(airquality)

Create Strip Chart in R


# strip chart for ozone reading of airquality dataset
stripchart(airquality$Ozone)

# add title, label, new color to strip chart


stripchart(airquality$Ozone, main="Mean ozone in parts per billion at
Roosevelt Island", xlab="Parts Per Billion", ylab="Ozone", col="orange")
main - adds the title "Mean ozone in parts per billion at Roosevelt Island"
xlab - adds the label "Parts Per Billion" for x-axis
ylab - add the label "Ozone" for y-axis
col = "Orange" - changes the color of strip to orange

Jitter Plot in R
Jitter plot is a variant of the strip plot with a better view of overlapping data
points. It is useful when there are large clusters of data points.

We pass method = "Jitter" inside the stripchart() method to create a strip chart
without overlapping of points.

stripchart(airquality$Ozone, main="Mean ozone in parts per billion at


Roosevelt Island", xlab="Parts Per
Billion", ylab="Ozone", col="orange", method = "jitter")

# create list of ozone and solar radiation reading of airquality dataset


list1 <- list("Ozone" = airquality$Ozone, "Solar Radiations" =
airquality$Solar.R)
stripchart(list1, main="Mean ozone in parts per billion at Roosevelt Island",
xlab="Parts Per Billion", col= c("orange","brown"), method = "jitter")

You might also like