How to Make a Stacked Bar Chart in R Using ggplot2

Brought to you by Jory Catalpa, Kyle Zrenchik, Yunxi Yang, University of Minnesota

To demonstrate how to make a stacked bar chart in R, we will be converting a frequency table into a plot using the package ggplot2.

Source: Table 1 from Prochaska, J. O., Velicer, W. F., Rossi, J. S., Goldstein, M. G., Marcus, B. H., Rakowski, W., … & Rossi, S. R. (1994). Stages of change and decisional balance for 12 problem behaviors. Health psychology, 13(1), 39.

Note that when we enter the data from the original table into a text file, we should create a new variable named “Stage Of Change”, and the value for each case will be “precontemplation”, “contemplation”, “preparation”, “action”, or “maintenance”. The final data will look like this in a text file:

Type of Behavior Sample Size Stage of Change
Smoking cessation 108 Precontemplation
Smoking cessation 187 Contemplation
Smoking cessation 0 Preparation
Smoking cessation 134 Action
Smoking cessation 247 Maintenance
Quitting cocaine 8 Precontemplation
Quitting cocaine 15 Contemplation
Quitting cocaine 0 Preparation
Quitting cocaine 71 Action
Quitting cocaine 62 Maintenance
Weight control 18 Precontemplation
Weight control 65 Contemplation
Weight control 0 Preparation
Weight control 22 Action
Weight control 18 Maintenance
High fat diet 41 Precontemplation
High fat diet 32 Contemplation
High fat diet 0 Preparation
High fat diet 5 Action
High fat diet 102 Maintenance
Adolescent delinquency 29 Precontemplation
Adolescent delinquency 46 Contemplation
Adolescent delinquency 0 Preparation
Adolescent delinquency 43 Action
Adolescent delinquency 41 Maintenance
Safer sex 94 Precontemplation
Safer sex 17 Contemplation
Safer sex 0 Preparation
Safer sex 102 Action
Safer sex 0 Maintenance
Condom use 131 Precontemplation
Condom use 58 Contemplation
Condom use 0 Preparation
Condom use 20 Action
Condom use 114 Maintenance
Sunscreen use 119 Precontemplation
Sunscreen use 18 Contemplation
Sunscreen use 0 Preparation
Sunscreen use 10 Action
Sunscreen use 80 Maintenance
Radon gas exposure 520 Precontemplation
Radon gas exposure 121 Contemplation
Radon gas exposure 0 Preparation
Radon gas exposure 57 Action
Radon gas exposure 0 Maintenance
Exercise acquisition 53 Precontemplation
Exercise acquisition 242 Contemplation
Exercise acquisition 182 Preparation
Exercise acquisition 101 Action
Exercise acquisition 139 Maintenance
Mammography screening 31 Precontemplation
Mammography screening 24 Contemplation
Mammography screening 0 Preparation
Mammography screening 26 Action
Mammography screening 60 Maintenance
Physicians' practices 43 Precontemplation
Physicians' practices 20 Contemplation
Physicians' practices 3 Preparation
Physicians' practices 2 Action
Physicians' practices 67 Maintenance

# Import data

TTM <- read.delim("G:/Fsos/Stats/11.1(2)/TTM.txt")
# Examine to ensure the data is correct

head(TTM)
##    Type.of.Behavior Sample.Size  Stage.of.Change
## 1 Smoking cessation         108 Precontemplation
## 2 Smoking cessation         187    Contemplation
## 3 Smoking cessation           0      Preparation
## 4 Smoking cessation         134           Action
## 5 Smoking cessation         247      Maintenance
## 6  Quitting cocaine           8 Precontemplation
tail(TTM)
##         Type.of.Behavior Sample.Size  Stage.of.Change
## 55 Mammography screening          60      Maintenance
## 56 Physicians' practices          43 Precontemplation
## 57 Physicians' practices          20    Contemplation
## 58 Physicians' practices           3      Preparation
## 59 Physicians' practices           2           Action
## 60 Physicians' practices          67      Maintenance
# Load package ggplot2

library(ggplot2)
# Plot the basic frame of the stacked bar chart.

ggplot(data = TTM, aes(x = Type.of.Behavior, y = Sample.Size, fill = Stage.of.Change)) + 
    geom_bar()

plot of chunk unnamed-chunk-4

# Flip the x axis and y axis so that the names of behavior can be seen
# clearly.

ggplot(data = TTM, aes(x = Type.of.Behavior, y = Sample.Size, fill = Stage.of.Change)) + 
    geom_bar() + coord_flip()

plot of chunk unnamed-chunk-5

# Reorder the chunks so that they represent the logical order of stage
# progression.

TTM$Stage.of.Change <- factor(TTM$Stage.of.Change, levels = c("Precontemplation", 
    "Contemplation", "Preparation", "Action", "Maintenance"))
# Re-plot the chart under the newly assigned order by changing the 'fill'
# command.

ggplot(data = TTM, aes(x = Type.of.Behavior, y = Sample.Size, fill = factor(Stage.of.Change))) + 
    geom_bar() + coord_flip()

plot of chunk unnamed-chunk-7

# Change the color palette so that it is easier to read.

ggplot(data = TTM, aes(x = Type.of.Behavior, y = Sample.Size, fill = factor(Stage.of.Change))) + 
    geom_bar() + coord_flip() + scale_fill_brewer(palette = 12)

plot of chunk unnamed-chunk-8

# Change the labels of the plot. Done!

ggplot(data = TTM, aes(x = Type.of.Behavior, y = Sample.Size, fill = factor(Stage.of.Change))) + 
    geom_bar() + coord_flip() + scale_fill_brewer(palette = 12) + labs(title = "Figure 1  Total Sample Size and Subsamples for Each Stage for Each of the 12 Problem Behaviors", 
    y = "Sample Size", x = "Type of Behavior", fill = "Stage of Change")

plot of chunk unnamed-chunk-9