youtube video link with explanations for these examples

https://youtu.be/MT_XkTeGJ_Y

We will be using the following packages

#Uncomment the following install statements and run them to install the packages(if  you do not have them  already installed)
#install.packages(ggplot2)
#install.packages(ggthemes)
#install.packages(scales)

library(ggplot2)
library(ggthemes)
library(scales)

Basic bar chart

# Notics that in this chart we only gave x = manufacturer. The y axis has not been defined.
pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar()
pl

# The reason is that by default the geom_bar used the frequence count of the x axis value. eg. in your dataset mpg it will count the manufacturers eg. how may rows have ford , toyota, honda etc
# The below code will produce exactly the same chart
pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar(stat ="count")  # By default the geom_bar will use the count for determining the height (y axis ) fir the bars
pl

Add title and labels

pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar()

pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

pl

Add a theme to the chart

Some themes are provided by ggplot and some additional themes are available via the ggthemes packages. In this example we have used a theme called theme_economist which is available in ggthemes package

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count", position = position_dodge2(preserve = "single"))

pl <- pl + ggthemes::theme_economist()  #  theme_economist has been used. there are so many built in themes you can use

pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")
pl

Adding data labels

You can adjusting the positioning of the text using vjust and the hjust options. In the example we have used vjust = -0.5 to slightly push the text labels above the bars. Try experimenting by putting values between -1 and 1. eg.( -0.9 or - 0 or 0.3 etc).

pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar(stat="count")

pl <- pl + geom_text(aes(label = ..count..), stat= "count",  vjust = -0.5)  # adding data labels

pl <- pl + ggthemes::theme_economist()
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

pl

Horizontal bar charts using coord_flip

By using the coord_flip option we can flip the chart to make it a horizontal bar chart

When you flip them you have to hjust instead of vjust. As your bars have become horizontal instead of being vertical.

pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar(stat="count")
pl <- pl + geom_text(aes(label = ..count..), stat= "count",  hjust = -0.5)  # adding data labels
pl <- pl + ggthemes::theme_economist()
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

pl <- pl + ggplot2::coord_flip()  # you can simply use coord_flip instead of ggplot2::coord_flip()

pl

Adding a line chart and a dot plot

We can add a line chart and a dot plot in the same chart if we wish.

pl <- ggplot(data = mpg,aes(x= manufacturer))
pl <- pl + geom_bar(stat="count")

pl <- pl + geom_line(stat = "count",group = 1, color ="red")
pl <- pl + geom_point(stat="count", colour ="red")

pl <- pl + geom_text(aes(label = ..count..), stat= "count",  hjust = -0.5)  # adding data labels
pl <- pl + ggthemes::theme_economist()
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

pl <- pl + ggplot2::coord_flip()  # you can simply use coord_flip instead of ggplot2::coord_flip()

pl

Adding a data field to change the fill color

We can add a line chart and a dot plot in the same chart if we wish.

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count")

#pl <- pl + geom_line(stat = "count",group = 1, color ="red")
#pl <- pl + geom_point(stat="count", colour ="red")

#pl <- pl + geom_text(aes(label = ..count..), stat= "count",  hjust = -0.5)  # adding data labels
pl <- pl + ggthemes::theme_economist()
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

#pl <- pl + ggplot2::coord_flip()  # you can simply use coord_flip instead of ggplot2::coord_flip()

pl

Rotating the x axis labels

We can add a line chart and a dot plot in the same chart if we wish.

Wrong way

In this example we have given a statement theme(axis.text.x = element_text(angle = 90,hjust =0 )) to change the angle of the x axis text to 90 degrees.

However in this example we have provided the name of the theme in the next line so our text does not rotate as expected. We should have given the theme customisation commands after we have defined our theme.

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count")


pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl <- pl + ggthemes::theme_economist()

pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="The x axis text did not rotate")
pl <- pl  + labs(x ="Car Brand", y = "Count")


pl

Correct way

Define your theme first and then define any theme customisation after that.

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count")

pl <- pl + ggthemes::theme_economist()   # define theme first
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 )) # now apply any customisation to this theme


pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="x axis text rotated by 90 degrees as expected")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")


pl

100% Stacked Bar Charts

When we use position =“fill” or position =position_fill() within the geom_bar statement we can convert our chart to a 100% stacked bar chart. See that we have used the library(scales) to get percentages on the y axis labels instead of the decimal values.

More details on stacked bar charts https://youtu.be/RPwJ6ExwPbg

library(scales)

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count", position = "fill")
#pl <- pl + geom_bar(stat="count", position = position_fill())
pl <- pl + ggthemes::theme_economist()
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Percentage")
pl <- pl + scale_y_continuous(labels = scales::percent)
#pl <- pl + ggplot2::coord_flip()
pl

Side by side bar chart

You would notice that the widths of the bars are not consistent. Some manufacturers have three class of cars(dodge), some have two (audi) and some have only one( pontiac).

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count", position = position_dodge())
#pl <- pl + geom_bar(stat="count", position = position_fill())
pl <- pl + ggthemes::theme_economist()
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

#pl <- pl + ggplot2::coord_flip()
pl

Fixing the widths of the bars

You would notice that the widths of the bars are not consistent. Some manufacturers have three class of cars(dodge), some have two (audi) and some have only one( pontiac). So if there is only bar then it occupies the same width and if there are three bars then those bars become less wide..

To fix this we can use position = position_dodge(preserve =“single”)

pl <- ggplot(data = mpg,aes(x= manufacturer, fill = class))
pl <- pl + geom_bar(stat="count", position = position_dodge(preserve ="single")) # preserve ="single makes consistent width bars
#pl <- pl + geom_bar(stat="count", position = position_fill())
pl <- pl + ggthemes::theme_economist()
pl <- pl  + theme(axis.text.x = element_text(angle = 90,hjust =0 ))
pl <- pl + labs(title ="My title")
pl <- pl + labs(subtitle ="My subtitle")
pl <- pl + labs(caption ="My caption")
pl <- pl  + labs(x ="Car Brand", y = "Count")

#pl <- pl + ggplot2::coord_flip()
pl

#Other data visualisation videos

https://youtube.com/playlist?list=PLkHcMTpvAaXV6Eg5ZHbcT2tA9-QnhJrIa