knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(foreign)
## How-to Graph

# Load Data ####
library(haven)
IV <- read_spss("C:/Users/monteangel/Desktop/SPSS_Data_V1.1.sav")
## Load libraries: tidyverse, ggplot
library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts -------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
ggplot2::mpg
## # A tibble: 234 x 11
##    manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
##    <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
##  1 audi         a4      1.8  1999     4 auto~ f        18    29 p     comp~
##  2 audi         a4      1.8  1999     4 manu~ f        21    29 p     comp~
##  3 audi         a4      2    2008     4 manu~ f        20    31 p     comp~
##  4 audi         a4      2    2008     4 auto~ f        21    30 p     comp~
##  5 audi         a4      2.8  1999     6 auto~ f        16    26 p     comp~
##  6 audi         a4      2.8  1999     6 manu~ f        18    26 p     comp~
##  7 audi         a4      3.1  2008     6 auto~ f        18    27 p     comp~
##  8 audi         a4 q~   1.8  1999     4 manu~ 4        18    26 p     comp~
##  9 audi         a4 q~   1.8  1999     4 auto~ 4        16    25 p     comp~
## 10 audi         a4 q~   2    2008     4 manu~ 4        20    28 p     comp~
## # ... with 224 more rows
## Simple Scatterplot
ggplot(IV) +
  geom_point(mapping = aes(x = Age, y = PTGISF_Total))
## Warning: Removed 30 rows containing missing values (geom_point).

## Scatterplot - R data
ggplot(data = mpg) +
  geom_point(mapping = aes(x = class, y = drv))

## With color
ggplot(IV, 
       aes(x = Age18_22, y = PTGISF_Total, color = CESSF_Total)) +
  geom_point()
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

## With size
ggplot(IV) +
  geom_point(mapping = aes(x = Age18_22, y = PTGISF_Total, size = CESSF_Total))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

## With transparent points
ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total, alpha = CESSF_Total))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

# With shapes
ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total, shape = Age))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: The shape palette can deal with a maximum of 6 discrete values
## because more than 6 becomes difficult to discriminate; you have
## 30. Consider specifying shapes manually if you must have them.
## Warning: Removed 76 rows containing missing values (geom_point).

# Setting aesthetic properties
ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total), color = "red")
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total), size = .2)
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total), shape = 11)
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

# Facet wrapping
ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total, color = "orange")) +
  facet_wrap(~ CESSF_Total, nrow = 2)
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

# Facet wrapping is a way of adding additional variables to a graph.
# Facet grid
ggplot(IV) +
  geom_point(aes(x = Age18_22, y = PTGISF_Total, color = "orange")) +
  facet_grid(Gender ~ CESSF_Total)
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

# Facet grids plot a combo of two variables on a plot.
# Graphing a smooth line
ggplot(IV) +
  geom_smooth(aes(x = Age_Shooting, y = PTGISF_Total))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 30 rows containing non-finite values (stat_smooth).
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 0.98
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1.02
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 0.98
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1.02
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other
## near singularities as well. 1

# Same as:
ggplot(IV) +
  geom_point(aes(x = Age_Shooting, y = PTGISF_Total))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing missing values (geom_point).

# With different linetypes
ggplot(mpg) +
  geom_smooth(aes(x = displ, y = hwy, linetype = drv))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# Adding labels
ggplot(IV) +
  geom_smooth(aes(x = Age_Shooting, y = PTGISF_Total, color = CESSF_Total)) +
labs(title = "PTG Score x Age During Shooting",
     subtitle = "Posttraumatic Growth Score x Age During the Shooting",
     x = "Age during shooting",
     y ="PTG score")
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 30 rows containing non-finite values (stat_smooth).
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 0.98
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1.02
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 0.98
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1.02
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other
## near singularities as well. 1

# Adding groups
ggplot(IV) +
  geom_smooth(aes(x = Age_Shooting, y = PTGISF_Total, group = Gender_Identity))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 30 rows containing non-finite values (stat_smooth).
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 0.98
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1.02
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 0.98
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1.02
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other
## near singularities as well. 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 0.98
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1.02
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 0.98
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1.02
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other
## near singularities as well. 1

# Each line represents a gender: male or female
ggplot(IV) +
  geom_smooth(aes(x = Age_Shooting, y = PTGISF_Total, color = Gender_Identity),
              show.legend = FALSE)
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 30 rows containing non-finite values (stat_smooth).
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 0.98
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1.02
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 0.98
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1.02
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other
## near singularities as well. 1

# This gets rid of the legend and colors lines based on gender.
# Multiple geoms
ggplot(IV) +
  geom_point(aes(x = Time_stayiv, y = PTGISF_Total)) +
  geom_smooth(aes(x = Time_stayiv, y = PTGISF_Total))
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 30 rows containing non-finite values (stat_smooth).
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 0.98
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 2.02
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 5.8758e-016
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 0.98
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 2.02
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 5.8758e-016
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : There are other
## near singularities as well. 1
## Warning: Removed 30 rows containing missing values (geom_point).

# Dotplot
ggplot(IV,
       aes(x = Age_Shooting)) +
  geom_dotplot(dotsize = .15)
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

# Histogram ####
ggplot(IV,
       aes(x = PTGISF_Total)) +
  geom_histogram(fill = "purple",
                 color = "red")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 30 rows containing non-finite values (stat_bin).

# Density
ggplot(IV,
       aes(x = PTGISF_Total)) +
  geom_density(fill = "cyan")
## Warning: Removed 30 rows containing non-finite values (stat_density).

# Violin plot
ggplot(IV,
       aes(x = 1,
           y = Time_leaveiv)) +
  geom_violin(fill = "magenta") +
  labs(title = "Decision to leave community")
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 15 rows containing non-finite values (stat_ydensity).

# Density plot
ggplot(IV) +
       stat_density_2d(aes(x = PTGISF_Total, y = CESSF_Total, fill = ..level..),
                       geom = "polygon") +
scale_fill_gradient(low = "blue",
                    high = "red") +
  labs(title = "Correlation of PTG and CES Scores")
## Warning: Removed 30 rows containing non-finite values (stat_density2d).

# Other density plot plot
ggplot(IV,
       aes(x = PTGISF_Total,
           y = Time_leaveiv)) +
  geom_density_2d(color = "orange") +
  labs(title = "Decision to leave community")
## Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous.
## Warning: Removed 30 rows containing non-finite values (stat_density2d).