Extra Learning Assignment - Making a Bar Graph with ggplot2

Kao Nou Moua, Sarah Thilmony, Tonya Cook

This is a tutorial of how to create a bar graph from a table using ggplot2 in RStudio. The table is Table 2. Characteristics of the Survey Participants. found in the following article:

Lu, E. Y., Lum, D., & Chen S. (2008). Cultural competency and achieving styles in clinical social work. Journal of Ethnic and Cultural Diversity in Social Work, 9(3-4), 1-32.

Part One: Create an Excel spreadsheet readable by RStudio from the published table.

  1. There are 18 independent items in Table 2. Therefore, create an Excel sheet with 18 rows.

  2. Add a column called “Percentage” with all of the 18 independent items from Table 2.

  3. Add another column of “Characteristics,” with the following coding:
    0 = Male
    1 = Female
    2 = Younger (35 & under)
    3 = Older (35 & over)
    4 = Less Education (18 & less)
    5 = Higher Education (18+)

  4. Add one more column labeled “Race,” with coding below. Note that these values will also be the labels that appear automatically in the legend on the right-hand side of the graph.
    0 = Asian (N = 64)
    1 = Non-Asian (N = 82)
    2 = Total (N = 146)

  5. Save the Excel sheet in .csv format and title the document “hw2.”

  6. Excel table will look like this:

##    Characteristics Percentage Race
## 1                0       29.5    1
## 2                1       70.5    1
## 3                2       49.2    1
## 4                3       50.8    1
## 5                4       60.3    1
## 6                5       39.7    1
## 7                0       42.0    2
## 8                1       58.0    2
## 9                2       29.6    2
## 10               3       70.4    2
## 11               4       64.0    2
## 12               5       36.0    2
## 13               0       36.6    3
## 14               1       63.4    3
## 15               2       38.2    3
## 16               3       61.8    3
## 17               4       62.5    3
## 18               5       37.5    3

Part Two: Import data into RStudio and create bar graph

  1. In RStudio, click on “Import Dataset” and choose “From Text File.” Choose the filepath for the csv file where you saved the Excel sheet.

  2. View header to view the first five rows of the table to ensure data looks correct.

    head(hw2)
    
  3. RStudio automatically treats the numeric values in the table as numeric. Because our values are categorical, we need to make the Race codes a factor.

    hw2$Race <- factor(hw2$Race, labels = c("Asian (n = 64)", "Non-Asian (n = 82)", "Total (n = 146)")) +
    
  4. RStudio automatically treats the numeric values in the table as numeric. Because our values are categorical, we need to make the Characteristics code a factor.

    hw2$Characteristics <- factor (hw2$Characteristics, labels = c("Male", "Female", "Younger (35 & under)", 
    "Older (35 & over)", "Less Education (18 & less)", "Higher Education (18+)")) +
    
  5. Load ggplot2 library

    library(ggplot2)
    
  6. Make a plot that uses the source data (data frame) of hw2. Plot the data of hw2 with the following global aesthetics: Characteristics on the x-axis Percentage on the y-axis, and fill the bars according to Race.

    ggplot(data = hw2, aes(x=Characteristics, y = Percentage, fill = (Race))) + 
    
  7. Create a bar graph using command geom_bar(), turn off default summary with the command stat = “identity”, position bars in graph side-to-side with the command position=position_dodge()

    geom_bar(stat = "identity", position=position_dodge()) +
    
  8. Flip x and y axis

    coord_flip() +
    
  9. Set minimum and maximum values of the Y axis

    scale_y_continuous(limits=c(0,100)) +
    
  10. Change background theme to black and white

    theme_bw()
    
  11. The bar graph will look like this:
    plot of chunk unnamed-chunk-2