Practice Set: Multipanel Figures & Adding To Your Plot

## here() starts at /Users/coop/Psych4175

Review of midus

Let’s take a preview of midus, just to remember what we’ve got. Remember, we added a “session” variable. (Don’t forget to scroll all the way over!)

ID sex age BMI physical_health_self mental_health_self self_esteem life_satisfaction hostility heart_self heart_father age_category session
1 10001 Male 61 26.263 2 4 42 7.750 5.5 No No old session1
2 10002 Male 69 24.077 5 5 34 8.250 6.0 No Yes old session2
6 10011 Female 52 25.991 5 4 41 7.000 5.5 No No middle session1
8 10015 Female 53 32.121 3 3 31 7.375 6.0 No Yes middle session2
10 10018 Male 49 22.499 4 4 41 8.500 6.0 No No middle session1
11 10019 Male 51 29.987 4 5 38 7.625 4.5 No No middle session2

Faceting

Faceting is an easy way to create sub-plots based on a categorical/factor variable. There are two main ways of faceting: - facet_grid will create a grid or matrix of sub-plots - facet_wrap will lay out each sub-plot kind of like a ribbon

You try! Imagine each of the 3740 midus observations were randomly assigned to belong to one of two groups. Add to the code below to facet based on session and this new group variable. The result should include 4 subplots. Organize them such that they are in a 2x2 layout, with group 1 in the left column, group 2 in the right column, session1 in the first row, and session2 in the second row.

You try! Now, use facet_wrap to get sub-plots reflecting all 3 factor variables (age_category, group, and session). How many sub-plots should you wind up with? Organize them such that there are 3 columns.

Adding Lines

You can use geom_vline and geom_hline to make vertical and horizontal lines, respectively. You’ll need to specify a x- or y-intercept.

Often, we want these lines to be based on summary statistics. This is where the tidyverse comes in very handy!

You try! Change the code below to represent the mean of self_esteem per age_category.

  • Plot these means as horizontal lines
  • HINT: You do NOT need to change the meansSelfEsteem code, only the plotting code
  • BONUS: How can you get rid of the legend that shows the line type?

Almost done…