Skip to main content

Ggplot2 package (part1)

It is a graphic package implemented in R, it allows to make graphs in a very flexible way because it contains many different commands to perform specific functions when creating and manipulating graphs. In addition, this graphic package has an underlying grammar in layers, so that it is composed of independent components. These independent components can be combined in many different ways to make or modify the graphs. Each graph is made up of a few equal components: data, visual marks that represent data points (geoms), and a coordinate system. The necessary variables for the ggplot command are the data to use in data frame format and know which variables we will use as x and y:
       ggplot (df, aes (x, y, ))
You can add more layers to the previous ggplot using the + operator, so the code is explicit as to which layers were added and in the order they were added.
here we will use the dataset iris:
head(iris)
##   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
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
Some examples:
  1. Plot using dots:
require('ggplot2')
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.1
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
  geom_point(aes(color=Species, shape = Species)) +
  xlab("Petal Length") +
  ylab("Petal Width") +
  ggtitle("IRIS1")
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
  geom_point(aes(color = Petal.Length, shape = Species), size = 2) +
  geom_vline(aes(xintercept = mean(Petal.Length)), color = "red", linetype = "longdash")+
  geom_hline(aes(yintercept = mean(Petal.Width)), color = "red", linetype = "longdash") +
  scale_color_gradient(low = "pink", high = "blue") +
  xlab("Petal Length") +
  ylab("Petal Width") +
  ggtitle("IRIS2")
  1. Dots with a grid:
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
  geom_point(aes(color=Species, shape = Species)) +
  facet_grid(~Species) +
  xlab("Petal Length") +
  ylab("Petal Width") +
  ggtitle("IRIS3")
  1. Density plots:
ggplot(iris, aes(Petal.Width, fill= Species, color = Species)) +
  geom_density(alpha = 0.5) +
  xlab("Petal Width") +
  ggtitle("IRIS4")
ggplot(iris, aes(Petal.Width, fill= Species, color = Species)) +
  geom_density(alpha = 0.5, position = 'stack') +
  xlab("Petal Width") +
  ggtitle("IRIS5")
  1. Boxplots:
ggplot(iris, aes(iris$Petal.Length, iris$Petal.Width)) +
  geom_boxplot(aes(fill=Species)) + 
  ylab("Sepal Length") +
  ggtitle("IRIS6")
ggplot(iris, aes(iris$Species, iris$Petal.Length)) +
  geom_boxplot(aes(fill=Species)) + 
  stat_summary(fun.y= max, geom="point", shape=2, size=3) +
  stat_summary(fun.y= min, geom="point", shape=1, size=3) +
  stat_summary(fun.y= mean, geom="point", shape=0, size=3) +
  xlab("Species") +
  ylab("Sepal Length") +
  ggtitle("IRIS7")

Popular posts from this blog

Support Vector Machines (SVM) in R (package 'kernlab')

Support Vector Machines (SVM) learning combines of both the instance-based nearest neighbor algorithm and the linear regression modeling. Support Vector Machines can be imagined as a surface that creates a boundary (hyperplane) between points of data plotted in multidimensional that represents examples and their feature values. Since it is likely that the line that leads to the greatest separation will generalize the best to the future data, SVM involves a search for the Maximum Margin Hyperplane (MMH) that creates the greatest separation between the 2 classes. If the data ara not linearly separable is used a slack variable, which creates a soft margin that allows some points to fall on the incorrect side of the margin. But, in many real-world applications, the relationship between variables are nonlinear. A key featureof the SVMs are their ability to map the problem to a higher dimension space using a process known as the Kernel trick, this involves a process of constructing ne

Initial Data Analysis (infert dataset)

Initial analysis is a very important step that should always be performed prior to analysing the data we are working with. The data we receive most of the time is messy and may contain mistakes that can lead us to wrong conclusions. Here we will use the dataset infert , that is already present in R. To get to know the data is very important to know the background and the meaning of each variable present in the dataset. Since infert is a dataset in R we can get information about the data using the following code: require(datasets) ?infert #gives us important info about the dataset inf <- infert #renamed dataset as 'inf' This gives us the following information: Format 1.Education: 0 = 0-5 years, 1 = 6-11 years, 2 = 12+ years 2.Age: Age in years of case 3.Parity: Count 4.Number of prior induced abortions: 0 = 0, 1 = 1, 2 = 2 or more 5.Case status: 1 = case 0 = control 6.Number of prior spontaneous abortions: 0 = 0, 1 = 1, 2

Ant Colony Optimization (part 2) : Graph optimization using ACO

The Travelling Salesman Problem (TSP) is one of the most famous problems in computer science for studying optimization, the objective is to find a complete route that connects all the nodes of a network, visiting them only once and returning to the starting point while minimizing the total distance of the route. The problem of the traveling agent has an important variation, and this depends on whether the distances between one node and another are symmetric or not, that is, that the distance between A and B is equal to the distance between B and A, since in practice is very unlikely to be so. The number of possible routes in a network is determined by the equation: (𝒏−𝟏)! This means that in a network of 5 nodes the number of probable routes is equal to (5-1)! = 24, and as the number of nodes increases, the number of possible routes grows factorially. In the case that the problem is symmetrical the number of possible routes is reduced to half: ( (𝒏−𝟏)! ) / 𝟐 The complexity o