Analytic bar graphs/charts

  by(wanga aron, for R and python sessions email harronjobswanga@gmail.com)

ANALYTIC BAR GRAPHS



In most statistical or data analysis projects there is no single one that I haven’t used a bar graph especially when I compare trends or  different grouped data. There is this old saying that goes “pictures speak louder than words”. That is exactly what statistical charts do.

Definition:

A bar graph is the representation of grouped data in rectangular bars with the height of the bars being proportional to the frequency or measurement under consideration. The width of the bars of different categories are usually equal.

Types of bar graphs

·         Horizontal bar graph

·        .Vertical bar graph

·         Grouped bar graph

·       . Stacked bar graph

Example:

When comparing the performance of male and female candidates in an examination the following table was generated.

 

Marks

Gender

20

M

30

f

40

m

50

f

60

M

70

F

80

M

90

F

100

F

49

F

56

M

78

M

 

Using python and R programming languages to draw the bar graphs

Python:

Make sure you install the following packages that I will use here to plot bar chart if you want to follow along this tutorial pandas,seaborn,matplotlib .

Run the code below on your ide either jupyter notebook, vs studio  , spyder or any other IDE you decide to use:

Code:

! pip install pandas, seaborn, matplotlib

 

Importing required libraries to our environment

Code:

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt

 

First, I will create a data frame in python capturing the above table

Code:

df=pd.DataFrame({"marks":[20,30,40,50,60,70,80,90,100,49,56,78],

                             "gender":["m","f","m","f","m","f","m","f","f","f","m","m"]})

print(df)

Output:

marks

gender

0

20

m

1

30

f

2

40

m

3

50

f

4

60

m

5

70

f

6

80

m

7

90

f

8

100

f

9

49

f

10

56

m

11

78

m

Now that we have created our dataframe we can begin ploting our bargraph

Using seaborn:

Code:

sns.barplot(x='gender',y='marks',data=df)

plt.title('Marks performance bar graph')

plt.show()

output:



 

R :

First we are going to install the required packages ggplot2,tidyverse

On your  Rstudio open a new rscript file and type in

Code:

install.packages(“ggplot2”)

Install.packages(“tidyverse”)

Thean we will load our packages into our environment;

Code:

load(“ggplot2” )

load(“tidyverse”)

The next step is to create a data frame in r

Code:

df<-data.frame(marks= c(20,30,40,50,60,70,80,90,100,49,56,78),gender=    c("m"       ,"f","m","f","m","f","m","f","f","f","m","m"))

Then we shall plot our barchart

Code:

ggplot(data=df,aes(x=gender,y=marks,fill=gender)) +geom_bar(stat = "identity")+labs(title = "Marks        gender bargraph")

 Output:



From the above plots we see that the marks scored by males is being compared to that of female and we notice that the height of the rectangular bar of female is higher than that of male. Here we can conclude that females candidates attained higher scores . 

 

 

Comments

Popular posts from this blog

Online data collection using python

IMPACTS AND A BRIEF HISTORY OF AI.

Most famous Statisticians of all time.