How to Plot All Variables in a Dataset



How to Plot All Variables in a Dataset

You will learn how to plot all variables in a data frame using the ggplot2 R package.

Prerequisites

Load required R package and set the default theme to theme_minimal() :

library(tidyverse)
theme_set(
  theme_minimal() +
    theme(legend.position = "top")
  )

Data preparation

  • Demo data:
head(iris, 3)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
  • Select numeric columns and gather them into key-value pairs:
iris.gathered <- iris %>%
  as_data_frame() %>%
  select_if(is.numeric) %>%
  gather(key = "variable", value = "value")

head(iris.gathered, 3)
## # A tibble: 3 x 2
##   variable     value
##   <chr>        <dbl>
## 1 Sepal.Length   5.1
## 2 Sepal.Length   4.9
## 3 Sepal.Length   4.7

Visualization

Plot the density distribution of each variable:

ggplot(iris.gathered, aes(value)) +
  geom_density() +
  facet_wrap(~variable)





No Comments

Post a Reply