Data Visualization — Bivariate Analysis in R

Greeshma Lakshmi
2 min readDec 9, 2021

--

I am working on R Programming as part of my course module. I was trying to visualize Boxplots for all feature variables against target variable in one plot. Initially I tried to find out an article or video on how to do that but I didn’t get complete information anywhere. Hence I started to figure it out from very basics.

Let us take the example of Iris dataset

#Loading the datasets package

library(datasets)

#Loading Iris Dataset

data(iris)

#to check summary of dataset

summary(iris)

summary(iris)

Feature variables are Sepal.Length, Sepal.Width, Petal.Length, Petal.Width and target variable is Species.

I know how to plot boxplot for a feature variable against target and here is how to do that.

Species_boxplot = ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot(aes(fill=Species))

Species_boxplot

Box plot of Sepal Length and Species

Now to implement it for all the other features, I just added all the features to Species_boxplot variable

Species_boxplot = ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot(aes(fill=Species)) +
ggplot(iris, aes(x=Species, y = Sepal.Width)) + geom_boxplot(aes(fill=Species)) +
ggplot(iris, aes(x=Species, y = Petal.Length)) + geom_boxplot(aes(fill=Species))+
ggplot(iris, aes(x=Species, y = Petal.Width)) + geom_boxplot(aes(fill=Species))

#After running above code in RStudio, run command below to view the plots.

Species_boxplot

Oops! I got the error below —

Error: Can’t add `ggplot(iris, aes(x = Species, y = Sepal.Width))` to a ggplot object.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
Can’t add `ggplot(iris, aes(x = Species, y = Sepal.Width))` to a ggplot object.
Backtrace:
1. ggplot2:::`+.gg`(…)
2. ggplot2:::add_ggplot(e1, e2, e2name)
4. ggplot2:::ggplot_add.default(object, p, objectname)
Run `rlang::last_trace()` to see the full context.

To solve this error, just load the library patchwork

library(patchwork)

Run the code again, you will be able to see boxplots for all feature variables against target.

I am pretty sure that there will be an alternative to do this, but I as tried this on my own, I thought of sharing.

Thank you and have a good day :)

--

--

Greeshma Lakshmi

An enthusiast in the field of Data Science and Technology