Basic graph#

Histogram, box plots and bar chart are the common type of plots for the continuous data.

Histogram#

# We can present the continuous data in a histogram.
# Let's make the histogram the "Sepal.Length" variable.

library (datasets)

histogram <- hist(iris$Sepal.Length, 
                  main = "Histogram of Sepal.Length", 
                  xlab = "Sepal.Length", 
                  col = "lightblue", 
                  border = "black")
_images/29cd408bd7e664edd6da39ee854e8eb03f9099c6043bb156430cb650fe4126d4.png
# We can also present the continuous data in a box plot.
# Let's make a box plot of the "Sepal.Length" variable.

box_plot <- boxplot(iris$Sepal.Length, 
                    main = "Box Plot of Sepal.Length", 
                    ylab = "Sepal.Length", 
                    col = "lightgreen", 
                    border = "black")
_images/93c202258029e85e58062f3fbd5baa1ac0d9e6b94575d1be6170be2ea3ecde15.png

Bar chart#

Bar chart is the common type of plot for the categorical data.

Let’s make a bar chart for the “Species” variable.

For making the bar chart, we need to count the frequency of each species.

species_counts <- table(iris$Species)

Now, let’s create a bar chart

barplot(species_counts,
        names.arg = levels(iris$Species),
        col = rainbow(length(levels(iris$Species))),
        main = 'Distribution of Iris Species',
        xlab = 'Species',
        ylab = 'Count')
_images/4a8cf90d5f6a22b6bd0830fbef6e5e98f857dc30bad3ef320ae620a8e71a7343.png

Scatter plot#

plot(iris$Sepal.Length, iris$Sepal.Width,
     main = "Sepal Length vs. Sepal Width",
     xlab = "Sepal Length", ylab = "Sepal Width",
     col = iris$Species, pch = 19)
# Adding legend
legend("topright", legend = levels(iris$Species), col = unique(iris$Species), pch = 19)
_images/f76a5c92ba16a5a0cb55e8b20b7e416b58240b66d6362dbc83b2dfbb10aa0a88.png

Heatmap#

# Heatmap of petal length and petal width

heatmap (table(iris$Petal.Length, iris$Petal.Width),
           dendrogram = 'none',
           main = "Petal Length vs. Petal Width Heatmap",
           xlab = "Petal Width", ylab = "Petal Length")
Warning message in plot.window(...):
""dendrogram" is not a graphical parameter"
Warning message in plot.xy(xy, type, ...):
""dendrogram" is not a graphical parameter"
Warning message in title(...):
""dendrogram" is not a graphical parameter"
_images/125f9fc8ecc5c72883d925a75489ddf92bc9ccb97df61d03911a460de596e9ac.png