Data Visualization Using R
Data visualization is the presentation of data in a pictorial or graphical format. It enables decision makers to see analytics presented visually, so they can grasp difficult concepts or identify new patterns.
Four methods of visualizing data are often used.
When visualizing a single numerical variable, a histogram will be our go-to tool, which can be created in R using the hist() function
hist(mtcars$mpg, xlab = "Miles/gallon", main = "Histogram of MPG (mtcars)", breaks = 12, col = "lightseagreen", border = "darkorange")
To know more about the arguments that a histogram can take check this link.
A barplot can provide a visual summary of a categorical variable, or a numeric variable with a finite number of values, like a ranking from 1 to 10. For drawing barplot I will use cyl varible which is nothing but Number of cylinders in mtcars dataset.
barplot(table(mtcars$cyl), xlab = "Number of cylinders", ylab = "Frequency", main = "mtcars dataset", col = "lightseagreen", border = "darkorange")
To know more about the arguments that a barplot can take check this link.
To visualize the relationship between a numerical and categorical variable, we can use a boxplot. Here mpg is a numerical variable and Number of cylinders is categorical.
boxplot(mpg ~ cyl , data = mtcars)
You can make the box plot more attractive by setting some of its parameters.
boxplot(mpg ~ cyl, data = mtcars,
xlab = "Number of cylinders",
ylab = "Miles/(US) gallon",
main = "Number of cylinders VS Miles/(US) gallon",
pch = 20,
cex = 2,
col = "lightseagreen",
border = "red")
You can use the above graph more beautiful by using the below code.
plot(mpg ~ disp, data = mtcars,
xlab = "Displacement",
ylab = "Miles Per Gallon",
main = "MPG vs Displacement",
pch = 20,
cex = 2,
col = "red")