Day 1 of 100days of code: Bar plot

#Blog post Feb 11,2019
#Day 1 of 100 ; 100 days code challenge
#I will try to plot some bar plots using dplyr and ggplot package and gapminder dataset
#installing the gapminder package for the gapminder dataset

install.packages(“gapminder”)

#loading the library and data

library(ggplot2) #as I already have installed the packages
library(dplyr)
library(gapminder)
data(“gapminder”)

#to view the headings of the data
head(gapminder)
#lets assign a varible for the gapminder data for ease of access

g <- gapminder

#lets view the head of the data

head(gapminder)

 A tibble: 396 x 6
   country     continent  year lifeExp      pop gdpPercap
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.
 2 Afghanistan Asia       1957    30.3  9240934      821.
 3 Afghanistan Asia       1962    32.0 10267083      853.
 4 Afghanistan Asia       1967    34.0 11537966      836.
 5 Afghanistan Asia       1972    36.1 13079460      740.
 6 Afghanistan Asia       1977    38.4 14880372      786.
 7 Afghanistan Asia       1982    39.9 12881816      978.
 8 Afghanistan Asia       1987    40.8 13867957      852.
 9 Afghanistan Asia       1992    41.7 16317921      649.
10 Afghanistan Asia       1997    41.8 22227415      635.

#Lets plot a bar chart for Life Expectancy for Asian countries

# %>% = pipe function can be used for augmenting multiple arguments in single line of code

g %>% filter(continent==”Asia” & year == 2007) %>% #we filtered the data only for Asia continent and year 2007 using filter function from dplyr pacakge
   head(5) %>%
   ggplot(aes(x= country, y =lifeExp)) +
   geom_bar(stat = “identity”, position = “identity”)

#the above code will produce something like this:

image

#lets twitch some of the codes: you can see the scale of axis y is only upto 60 years but the data goes beyond 70 years, so we want the axis to show the values upto 80, so what we need to do is; insert one more argument of scale_y_continuous, and we also want to change the label of y axis; now the code will look like this:

g %>% filter(continent==”Asia” & year == 2007) %>%
   head(5) %>%
   ggplot(aes(x= country, y =lifeExp)) +
   geom_bar(stat = “identity”, position = “identity”) +
   scale_y_continuous(name=”Life Expectancy”, limits=c(0, 80))

image

#Now lets say, I am not quite happy with the theme, I want something classical, so there is also a classical theme for the ggplot graphs, now the code will look like this:

g %>% filter(continent==”Asia” & year == 2007) %>%
   head(5) %>%
   ggplot(aes(x= country, y =lifeExp)) +
   geom_bar(stat = “identity”, position = “identity”) +
   scale_y_continuous(name=”Life Expectancy”, limits=c(0, 80)) +
  theme_classic()

image

#now say, I am not happy with color of the bars, I want to change them. So, what I need to do is , fill it with some different colors, you can use color function to see what are the options available for you.

colors()

 [1] "white"                "aliceblue"            "antiquewhite"         "antiquewhite1"       
  [5] "antiquewhite2"        "antiquewhite3"        "antiquewhite4"        "aquamarine"          
  [9] "aquamarine1"          "aquamarine2"          "aquamarine3"          "aquamarine4"         
 [13] "azure"                "azure1"               "azure2"               "azure3"              
 [17] "azure4"               "beige"                "bisque"               "bisque1" 

#now lets change the color of the plot, I really like red colors, so included one fill function in geom_bar argument

g %>% filter(continent==”Asia” & year == 2007) %>%
   head(5) %>%
   ggplot(aes(x= country, y =lifeExp)) +
   geom_bar(stat = “identity”, position = “identity”, fill = “red”) +
   scale_y_continuous(name=”Life Expectancy”, limits=c(0, 80)) +
   theme_classic()

image

#now lets say, you are not happy with the width of the bars, you need smaller bars in the plot

g %>% filter(continent==”Asia” & year == 2007) %>%
   head(5) %>%
   ggplot(aes(x= country, y =lifeExp)) +
   geom_bar(stat = “identity”, position = “identity”, fill = “red”, width = 0.5) +
   scale_y_continuous(name=”Life Expectancy”, limits=c(0, 80)) +
   theme_classic()

image

#now lets say, I want to see the 5 countries from Europe with their life Expectancy, so I will filter my data for Europe. and this time may be I will use something purple for the bars. so the codes will be like;

g %>% filter(continent==”Europe” & year == 2007) %>%
   head(5) %>%
   ggplot(aes(x= country, y =lifeExp)) +
   geom_bar(stat = “identity”, position = “identity”, fill = “orchid”, width = 0.5) +
   scale_y_continuous(name=”Life Expectancy”, limits=c(0, 80)) +
   theme_classic()

image

Thank you. Hope this will help you out.

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