── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl) # package to open excel fileslibrary(plotly) # a graphics package, and alternative to ggplot2
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
survey <-read_excel("GSS.xlsx")
Create a histogram for race.
survey |>mutate(race =as_factor(race)) |>mutate(race =fct_recode(race,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web",NULL =".i: Inapplicable")) |>drop_na(race) |>mutate(race =fct_infreq(race)) |>plot_ly(x =~race) |>add_histogram()
This histogram shows what the race is of the survey participants. The largest group of participants identified themselves as white.
Create a histogram for spanking.
survey |>mutate(spanking =as_factor(spanking)) |>mutate(spanking =fct_recode(spanking,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web",NULL =".i: Inapplicable")) |>drop_na(spanking) |>mutate(spanking =fct_infreq(spanking)) |>plot_ly(x =~spanking) |>add_histogram()
This histogram shows how survey participants feel about spanking their children.The largest population exists among those who agree with spanking and the smallest population resides among the strongly disagree with spanking.
Convert spanking to a numeric variable and create a boxplot for race and spanking.
survey |>mutate(race =as_factor(race)) |>mutate(race =fct_recode(race,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web",NULL =".i: Inapplicable")) |>drop_na(race) |>mutate(race =fct_infreq(race)) |>mutate(spanking =as_factor(spanking)) |>mutate(spanking =fct_recode(spanking,NULL =".d: Do not Know/Cannot Choose",NULL =".i: Inapplicable",NULL =".n: No answer",NULL =".s: Skipped on Web")) |>drop_na(spanking) |>mutate(spanking =as.numeric(spanking)) |>plot_ly(x =~race, y =~spanking) |>add_boxplot()
This box plot compares how the survery particpants feel about spanking and what their race is.
Convert childs to a numeric variable; be sure to change ‘8 or more’ to 8. Create a histogram for childs.
survey |>mutate(childs =as_factor(childs)) |>mutate(childs =fct_recode(childs,NULL =".d: Do not Know/Cannot Choose",NULL =".s: Skipped on Web","8"="8 or more")) |>drop_na(childs) |>mutate(childs =as.numeric(childs)) |>plot_ly(x =~childs) |>add_histogram()
The histogram shows how often the survey participants had a certain number of children. The most common amount of children is one and the least common number of children is ten.
Create a box plot with spanking as the categorical variable and childs as the numeric variable.
survey |>mutate(spanking =as_factor(spanking)) |>mutate(spanking =fct_recode(spanking,NULL =".d: Do not Know/Cannot Choose",NULL =".i: Inapplicable",NULL =".n: No answer",NULL =".s: Skipped on Web",)) |>drop_na(spanking) |>mutate(spanking =fct_infreq(spanking)) |>mutate(childs =as_factor(childs)) |>drop_na(childs) |>mutate(childs =as.numeric(childs)) |>plot_ly(x =~spanking, y =~childs) |>add_boxplot()
This box plot shows that the sentiment around spanking is similar depending on how many children there are.
Create a histogram for hapmar.
survey |>mutate(hapmar =as_factor(hapmar)) |>mutate(hapmar =fct_recode(hapmar,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web",NULL =".i: Inapplicable",)) |>drop_na(childs) |>plot_ly(x =~hapmar) |>add_histogram()
Warning: Ignoring 4251 observations
This histogram shows how they felt about their marriage. The largest group were very happy in their marriage with the smallest group being not too happy.
Create a histogram of class (order = lower, working, middle, upper).
survey |>mutate(class_ =as_factor(class_)) |>mutate(class_ =fct_recode(class_,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web")) |>mutate(class_ =fct_relevel(class_, "Lower class", "Working class", "Middle class", "Upper class")) |>drop_na(class_) |>plot_ly(x =~class_) |>add_histogram()
This histogram shows how the participants identified the social class they were a part of. The middle class was the largest group and the upper class was the smallest.
Create another histogram of class, and set hapmar to different color bars for each class.
survey |>mutate(class_ =as_factor(class_)) |>mutate(class_ =fct_recode(class_,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web")) |>mutate(class_ =fct_relevel(class_, "Lower class", "Working class", "Middle class", "Upper class")) |>drop_na(class_) |>mutate(hapmar =as_factor(hapmar)) |>mutate(hapmar =fct_recode(hapmar,NULL =".d: Do not Know/Cannot Choose",NULL =".n: No answer",NULL =".s: Skipped on Web",NULL =".i: Inapplicable",)) |>mutate(hapmar =fct_relevel(hapmar, "NOT TOO HAPPY", "PRETTY HAPPY", "VERY HAPPY")) |>drop_na(class_) |>plot_ly(x =~class_, color =~hapmar) |>add_histogram()
This histogram shows what social class each surveyed person identified as and what their correlating level of happiness was. In each class the not too happy portion of people was the lowest. In all classes, except for the lower class, more people reported being very happy rather than pretty happy.
Using fct_collapse(), group age into the following groups: under 30, 30s, 40s, 50s, 60s, 70s, and 80 and up. Create a histogram.
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_relevel(...)`.
Caused by warning:
! 1 unknown level in `f`: 80 and up
This histogram shows the amount of people surveyed and groups them into their decade age range. The age range who had the most people surveyed was people in their 30s and the least age range was people 80 years old and higher.
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_relevel(...)`.
Caused by warning:
! 1 unknown level in `f`: 80 and up
The heatmap above shows how many people fall into a certain age range and their overall level of happiness. The darker the area correlating to an certain age and level of happiness states that there are few people that fall within those correlating features. The lighter an area is the more people falling within those parameters. The most populated area is people in their 30s who are very happy and the least populated is people are and 80 and up and not too happy.