
Ggplot2 is based on the “grammar of graphics”, which provides a standard way to describe the components of a graph (the “gg” in ggplot2 refers to the grammar of graphics). We also want to colour the bars differently based on the continent. Note that we want two bars per country - one of these should be the life expectancy in 1952 and the other in 2007. Here is a rough sketch to get us started on what we can do: We will use a bar plot to communicate this information graphically because we can easily see the levels of the life expectancy variable, and compare values over time and across countries. We also want to group the countries by continent. Specifically, we want to see the life expectancy in each of these countries in 19. We would like to show the change in life expectancy from 1952 to 2007 for 11 (arbitrarily-selected) countries: Bolivia, China, Ethiopia, Guatemala, Haiti, India, Kenya, Pakistan, Sri Lanka, Tanzania, Uganda. This dataset is an excerpt from the GapMinder data, and it shows the life expectancy, population and GDP per capita of various countries over 12 years between 1952 to 2007. We will be using the GapMinder dataset that comes pre-packaged with R.
#Bar graph r studio code
All code is commented so this should be straightforward to follow even if you have not used dplyr before. I also use the dplyr package to clean data. This post assumes basic familiarity with the following R concepts:

It can be difficult for a beginner to tie all this information together.
#Bar graph r studio how to
The grouping column was converted to be the variables of our matrix and the subgroups are now specified by the row names.There is a wealth of information on the philosophy of ggplot2, how to get started with ggplot2, and how to customize the smallest elements of a graphic using ggplot2 - but it’s all in different corners of the Internet. It shows our matrix that we will use to draw a Base R barchart. Have a look at the previous output of the RStudio console. Row.names(data_base) <- data_base$subgroupĭata_base <- data_baseĬolnames(data_base) <- c("group 1", "group 2", "group 3") names (data_base ) <- data_base$subgroupĭata_base <- data_base Ĭolnames (data_base ) <- c ( "group 1", "group 2", "group 3" )ĭata_base # Print modified data # group 1 group 2 group 3 # A 4 3 7 # B 1 6 3ĭata_base <- reshape(data, # Modify data for Base R barplot In this example, I’ll show how to use the basic installation of the R programming language to draw a barplot with groups.įor this, we first have to convert our data frame to a properly formatted matrix:ĭata_base <- reshape (data, # Modify data for Base R barplot So keep on reading!Įxample 1: Drawing Grouped Barchart Using Base R The following examples show three different alternatives on how to draw grouped barplots in R. The variable values contains the height of our bars, the variable group defines three different groups, and the variable subgroup divides each of our three main groups into two subgroups A and B. It shows that our example data has six rows and three columns.


frame (values = c ( 4, 1, 3, 6, 7, 3 ), # Create example data group = rep (c ( "group 1",ĭata # Print example data # values group subgroup # 1 4 group 1 A # 2 1 group 1 B # 3 3 group 2 A # 4 6 group 2 B # 5 7 group 3 A # 6 3 group 3 Bĭata <- ame(values = c(4, 1, 3, 6, 7, 3), # Create example data
