Learn R-101 in easy way V#5
Zahid Asghar
Libraries
1
Recall relevant libraries. In this post we require dplyr
and ggolot2
which are available in tidyverse
. For more background on it , please watch V#1 to V#4 VIDEO
. These videos are in Urdu/Hindi so appologies from others but codes are available to all for reproducing all the contents. Complete code is given in video link for bar graph here VIDEO
Outline
bar graph: single categorical variable
Histogram : for continuous variable
bar graph in ggplot2
1 ggplot (mtcars) +
2 aes (x = as.factor (cyl)) +
3 geom_bar ()
1
Read data in ggplot2
2
read data and define aesthetics
3
Mention which graph you want to make. I am using geom_bar as I have single categorical variable. If I want scatter plot, I will use geom_point()
color="blue"
means border of bar graph while fill
means complete bar
ggplot (mtcars)+
aes (x= as.factor (cyl))+
geom_bar (color= "blue" ) # Color is the border
ggplot (mtcars)+
aes (x= as.factor (cyl))+
geom_bar (fill= "blue" )
3: Using Hue
ggplot (mtcars, aes (x= as.factor (cyl), fill= as.factor (cyl) )) +
geom_bar ( )+
scale_fill_hue (c = 40 ) +
theme (legend.position= "none" )
4: Using RColorBrewer
ggplot (mtcars, aes (x= as.factor (cyl), fill= as.factor (cyl) )) +
geom_bar ( ) +
scale_fill_brewer (palette = "Set1" ) +
theme (legend.position= "none" )
5: Using greyscale:
ggplot (mtcars, aes (x= as.factor (cyl), fill= as.factor (cyl) )) +
geom_bar ( ) +
scale_fill_grey (start = 0.25 , end = 0.75 ) +
theme (legend.position= "none" )
6: Set manualy
ggplot (mtcars, aes (x= as.factor (cyl), fill= as.factor (cyl) )) +
geom_bar ( ) +
scale_fill_manual (values = c ("lightgreen" , "lightgreen" , "blue" ) ) +
theme (legend.position= "none" )
Create data
data <- data.frame (
name= c ("A" ,"B" ,"C" ,"D" ,"E" ) ,
value= c (3 ,12 ,5 ,18 ,45 )
)
1 ggplot (data) +
2 aes (x= name, y= value) +
3 geom_bar (stat = "identity" ) +
coord_flip ()
1
data
2
aes
thetics
3
geom_bar 4 flip
coordinates
Create 2023 Census data
data <- data.frame (
name= c ("Karachi" ,
"Hyderabad" ,
"Larkana" ,
" Mir Pur Khas" ,
"Shaheed Benazir Abad" ,
"Sukhur"
) ,
value= c (19124000 ,
12896000 ,
8190926 ,
5172132 ,
6299600 ,
6736027
)
)
data <- data |> mutate (population= value/ 1000000 )
ggplot (data, aes (x= name, y= population))+
geom_bar (stat = "identity" , width= 0.9 ) +
theme_minimal ()+
geom_text (aes (label= population), vjust= - .5 )
ggplot (data, aes (x= name, y= population)) +
geom_bar (stat = "identity" , width= 0.9 , fill= "blue" ) +
theme_minimal ()+
geom_text (aes (label= population), vjust= - .5 )
Some more modifications
ggplot (data,aes (x= reorder (name,- population),y= population))+ geom_bar (stat= "identity" , fill= "blue" , width = 0.8 )+
theme_minimal ()+
geom_text (aes (label= population), vjust= - 0.5 ) +
labs (x= element_blank (),
y= element_blank ())
ggplot (data,aes (x= reorder (name,population),y= population))+ geom_bar (stat= "identity" , fill= "blue" , width = 0.8 )+
theme_minimal ()+
geom_text (aes (label= population), hjust= 1 , col= "white" , size= 5 ) +
labs (x= element_blank (),
y= element_blank ()) + coord_flip ()
library (ggthemes)
ggplot (data, aes (x = reorder (name, population), y = population)) + geom_bar (stat =
"identity" ,
fill = "blue" ,
width = 0.8 ) +
theme_minimal () +
geom_text (
aes (label = population),
hjust = 1 ,
col = "white" ,
size = 5
) +
labs (x = element_blank (),
y = element_blank ()) + coord_flip () +
labs (title = "Sind population (in millions) division wise: 2023 census" ,
caption = "Wikipedia" ) +
theme_clean (
)
This is a work by Zahid Asghar , any feedback is highly encouraged.