Diverging Bar Chart in R
Last Updated :
09 Jan, 2023
Diverging Bar Chart is a kind of bar chart which are mostly used to compare two or more variables that are plotted on different sides of the middle bar. In diverging bar plot, the positive charts are plotted on the right side of the diverging(middle) axis and negative values are placed on the other side (left) of the diverging axis. IN R Programming we can implement them using the ggplot() function which is present in the ggplot2 package.
Syntax: ggplot(df,aes,fill,y)+geom_bar()+coord_flip()
Where,
- df – data frame that needs to be plotted
- aes – aesthetics of the plot are given here( how to order and plot the values of data frame)
- fill – used to specify the value to be filled in the data frame
- y – used to specify the labels that need to display on y-axis
- geom_bar() – The inclusion of this is to plot the bar graph
- coord_flip() – to rotate the plot and make y as primary axis [ By default the bar graph is plotted along x as the primary axis]
Before starting, we will be requiring the ggplot module and the dataset. For that, run the code given below.
R
install.packages ( "ggplot2" )
library (ggplot2)
df <- data.frame ( letters = LETTERS [1:26],
value= rnorm (26))
|
Once, we load the module and the dataset, we can now move ahead with the diverging bar chart.
Diverging Bar Chart
In this, we will plot the bar chart for the dataset and set the width as 0.5.
R
ggplot (df, aes (x= reorder ( letters ,value),
fill=value,y=value))+
geom_bar (stat= 'identity' ,width=0.5)
|
Output:
Customize Diverging Bar Plot:
If we observe above plot which is less interactive, so we are customizing the above plot so that we can understand more about the plot by labelling each and every part of it. So the steps in customizing the above plot are as follows…
Changing width and flipping coordinates
Using this, we can change the width of the bar and also flip the axes.
Syntax: geom_bar(width)+coord_flip()
where. width is used to specify the width of bar plot.
R
ggplot (df, aes (x= reorder ( letters ,value),fill=value,y=value))+
geom_bar (stat= 'identity' ,width=0.8)+ coord_flip ()
|
Output :
Mapping values
After plotting the bar plot we need to label each and every bar based on its value to do so we need to use geom_text() which is used to add text labels by specifying the aesthetics of the plot.
Syntax: geom_text(mapping)
where mapping is used to define the aesthetics of the plot we want to label using aes() function.
R
ggplot (df, aes (x= reorder ( letters ,value),fill=value,y=value))+
geom_bar (stat= 'identity' ,width=0.5)+ coord_flip ()+
geom_text ( aes (label= round (value,2)))
|
Output:
Adding labels and title
After adding labels to each bar we can also specify x, y -axis labels and title of the plot.
R
ggplot (df, aes (x= reorder ( letters ,value),fill=value,y=value))+
geom_bar (stat= 'identity' ,width=0.5)+ coord_flip ()+
xlab ( "Letters" )+ ylab ( "Values" )+ labs (title= "Customized Diverging Bar Plot" )
|
Output:
Scaling
To add limits to the axis of the plot we need to use scale_y_continuous() function(as our primary axis is y-axis)
Syntax: scale_y_continuous(breaks,limits)
where,
- breaks – used to specify breaks of the plot by defining the difference between them(min,max,by=difference)
- limits -used to specify limits of the plot based on the min and max values of bars
R
ggplot (df, aes (x= reorder ( letters ,value),fill=value,y=value))+
geom_bar (stat= 'identity' ,width=0.5)+ coord_flip ()+
scale_y_continuous (breaks= seq (-2, 2, by = 1),
limits = c ( min (df$value) - 0.5, max (df$value) + 0.5))
|
Output :
Fill Gradient
Finally we also customize the color of the plot using gradient colors using scale_fill_gradient2() function in ggplot package. This function is going to apply gradient colors based on the obtained range of values.
Syntax: scale_fill_gradient2(low,mid,high)
R
ggplot (df, aes (x= reorder ( letters ,value),fill=value,y=value))+
geom_bar (stat= 'identity' ,width=0.5)+ coord_flip ()+
scale_fill_gradient2 (low = "red" ,mid = "purple" ,high = "green" )+
coord_flip ()
|
Output:
Share your thoughts in the comments
Please Login to comment...