1 Goal


The goal of this tutorial is to create a line plot having categorical data in the x axis.


2 Loading libraries


library(dplyr)
library(ggplot2)
library(reshape2)

3 Data import


# In this tutorial we are going to use the dataset iris
data(iris)
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

4 Data preparation


# We are going to take the average of every variable per species
my_group <- group_by(iris, Species)
iris_mean <- summarise_all(my_group, mean)
iris_mean
## # A tibble: 3 x 5
##   Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
##   <fct>             <dbl>       <dbl>        <dbl>       <dbl>
## 1 setosa             5.01        3.43         1.46       0.246
## 2 versicolor         5.94        2.77         4.26       1.33 
## 3 virginica          6.59        2.97         5.55       2.03
# Now we melt the table
iris_melt <- melt(iris_mean, id = "Species")
iris_melt
##       Species     variable value
## 1      setosa Sepal.Length 5.006
## 2  versicolor Sepal.Length 5.936
## 3   virginica Sepal.Length 6.588
## 4      setosa  Sepal.Width 3.428
## 5  versicolor  Sepal.Width 2.770
## 6   virginica  Sepal.Width 2.974
## 7      setosa Petal.Length 1.462
## 8  versicolor Petal.Length 4.260
## 9   virginica Petal.Length 5.552
## 10     setosa  Petal.Width 0.246
## 11 versicolor  Petal.Width 1.326
## 12  virginica  Petal.Width 2.026

5 Making a line plot for every characteristic


ggplot(data = iris_melt) +
  geom_line(aes(x = Species, y = value, colour = variable, group = variable))


6 Conclusion


In this tutorial we have learnt how to make line plots using categorical data. It is important to prepare the dataset properly and to use the group function for drawing the proper line.