A well-constructed plot can be more effective than a table in communicating key points of what authors seem to communicate about their data. This is especially true for showing comparisons or trends.
Below we demonstrate how to take information from a table and depict it in a plot using ggplot2.
The example is from Bergin, A. E. (1991). Values and religious issues in psychotherapy and mental health. American Psychologist, 46(4), 394-403. We used Table 2, Religious Preferences of Professional Groups Versus The Public At Large.
We started by putting the data from Table 2 into a .csv file in Excel. We then uploaded the data into R and named the dataset “Spirituality”.
Spirituality <- read.csv("~/Desktop/Percent_Professionals.csv")
We viewed the data using the head() and tail() functions to make sure it looked like we wanted it to.
head(Spirituality)
## Religion Prof Percent
## 1 Protestant MFT 49
## 2 Jewish MFT 12
## 3 Catholic MFT 14
## 4 Nonreligious MFT 15
## 5 Other MFT 9
## 6 Protestant SW 40
tail(Spirituality)
## Religion Prof Percent
## 25 Other Total 8
## 26 Protestant General 57
## 27 Jewish General 2
## 28 Catholic General 2
## 29 Nonreligious General 9
## 30 Other General 4
And we created factors to re-order the variables so they matched the order in Table 2.
Spirituality$Prof2 <- factor(Spirituality$Prof, c("MFT", "SW", "Psychiatrist",
"Psychologist", "Total", "General"))
Spirituality$Religion2 <- factor(Spirituality$Religion, c("Protestant", "Jewish",
"Catholic", "Nonreligious", "Other"))
We opened the packages ggplot2 and RColorBrewer. (If you haven't installed these packages before, you'll need to do that first.)
library(ggplot2)
library("RColorBrewer")
And now to the fun part! We used the code below to plot the Spirituality data. You will see that we used geom_point and geom_line to specify the type of plot we wanted (i.e., a scatterplot with lines connecting each point).
We did the following:
## Make Plot
ggplot(data = Spirituality, aes(x = Spirituality$Prof2, y = Percent, color = Spirituality$Religion2)) +
geom_point(size = 3) + geom_line(aes(group = Spirituality$Religion2), names.arg = (c)) +
theme_bw(14) + theme(axis.text.x = element_text(angle = -45, hjust = 0)) +
labs(x = "Mental Health Professionals vs. General Population", color = "Religious Preference") +
scale_color_brewer(palette = "Set1")
We hope this was helpful for all of you. Happy plotting and good luck!