Day 2 of 100 days of Code: Bar plot 2: legend and labels in ggplot

#Day 2 of 100days of code
#Lets improve the bar plots and look into the other features we can use
#I will use the irish database for the plots

data(“iris”)
View(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

#I will use ggplot2, dplyr and reshape2 for plotting, filtering and data arrangements
#you can use the install.packages function to install the pacakges

install.packages(“ggplot2”)
install.packages(“dplyr”)
install.packages(“reshape2”)


#then load the library with library function

library(ggplot2)

library(dplyr)

library(reshape2)
#lets do some data reshaping with the reshape2 package
#I will use melt function with Species ID to arrange the data in column according to their species and variables

iris2 <- melt(iris, ID = “species”)
head(iris2)

Species     variable value
1  setosa Sepal.Length   5.1
2  setosa Sepal.Length   4.9
3  setosa Sepal.Length   4.7
4  setosa Sepal.Length   4.6
5  setosa Sepal.Length   5.0
6  setosa Sepal.Length   5.4

#lets say

I <- iris2


#plotting: will plot a bar graph where the values will be side by side or in dodge position

I %>% ggplot(aes(x = Species, y = value)) + geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable))

image

#now lets say I am not happy with the legend position and I want it on the top of the plot

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = “top”)

image

#now lets say I am not happy with the legend position and I want it on the bottom of the plot

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = “bottom”)

image

#now lets say I am not happy with the legend position and I want it on some specified place(you can use coordinate to change the positon also)

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = c(0.1, 0.9))

image

#lets say I want to change the variable in legend to attributes

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = c(0.1, 0.9)) +
                                    labs(fill = “Attributes”)

image

#lets say i want to add plot tittle and change the x and y legends

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = c(0.1, 0.9)) +
   labs(title = “Iris Dataset”, fill = “Attributes”, x = “Flower species”, y = “Length and width parameter”)

image

#By default the title are place in left alignment in ggplot, lets say I want the title to be center placed

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = c(0.1, 0.9), plot.title = element_text(hjust = 0.5)) +
   labs(title = “Iris Dataset”, fill = “Attributes”, x = “Flower species”, y = “Length and width parameter”)

image

#you can also change the appearance of main title and axis labels by modifying the theme arguments under the element_text () function:

  • family : font family
  • face : font face. “plain”, “italic”, “bold” and “bold.italic”
  • color : text color
  • size : text size in pts
  • hjust : horizontal justification (in [0, 1])
  • vjust : vertical justification (in [0, 1])
  • line height : line height. In multi-line text, the line height argument is used to change the spacing between lines.
  • color : an alias for color

For example:

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “dodge”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = c(0.1, 0.9), plot.title = element_text(hjust = 0.5, size = 14, color = “red”, face = “bold”), axis.title.x = element_text(color = “red”)) +
   labs(title = “Iris Dataset”, fill = “Attributes”, x = “Flower species”, y = “Length and width parameter”)

image

#Lets say you want to plot a stack bar , you have to change the position from dodge to stack:

I %>% ggplot(aes(x = Species, y = value)) +
   geom_bar(position = “stack”, stat = “identity”, aes(fill = variable)) +
   theme(legend.position = c(0.1, 0.9), plot.title = element_text(hjust = 0.5, size = 14, color = “red”, face = “bold”), axis.title.x = element_text(color = “red”)) +
   labs(title = “Iris Dataset”, fill = “Attributes”, x = “Flower species”, y = “Length and width parameter”)

image

#play with your codes and have fun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s