Maria Mendoza - Project 1 - US Workforce Data by State
Introduction to Dataset
The dataset I will be working on is information about the United States workforce organized by the 50 states and includes Washington, D.C. for the year 2018. The dataset was retrieved from Open Data website from the State of Maryland. It has ten variables, one of which is qualitative and the other nine are quantitative. The qualitative variable is State, and the nine quantitative variables are Civilian Labor Force, Employment, Unemployment, Unemployment Rate, Labor Force Participation Rate (Total), Labor Force Participation Rate (Male), Labor Force Participation Rate (Female), Employment in STEM Occupations, and Doctoral Scientists and Engineers per 100,000 Workers.
With this data, I want to see which states have the highest employment in STEM occupations, which I plan to show with a scatterplot graph. I also want to compare a few states to see the difference between male and female percentages labor force participation.
Access Library Packages, load dataset, and view the data
library(dslabs)library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.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
Rows: 55 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (10): State, Civilian Labor Force, Employment, Unemployment, Unemploymen...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(states_workforce) # shows the first few lines of data
# A tibble: 6 × 10
State `Civilian Labor Force` Employment Unemployment Unemployment Rate (%…¹
<chr> <chr> <chr> <chr> <chr>
1 Alabama 2199000 2112000 86000 3.9
2 Alaska 357000 333000 24000 6.6
3 Arizona 3440000 3274000 166000 4.8
4 Arkansas 1351000 1301000 50000 3.7
5 Califor… 19398000 18583000 815000 4.2
6 Colorado 3096000 2995000 102000 3.3
# ℹ abbreviated name: ¹`Unemployment Rate (%)`
# ℹ 5 more variables: `Labor Force Participation Rate, Total (%)` <chr>,
# `Labor Force Participation Rate, Male (%)` <chr>,
# `Labor Force Participation Rate, Female (%)` <chr>,
# `Employment in STEM Occupations (%)` <chr>,
# `Doctoral Scientists and Engineers per 100,000 Workers` <chr>
glimpse(states_workforce) # check number of rows and columns
Rows: 55
Columns: 10
$ State <chr> "Alabama", "Al…
$ `Civilian Labor Force` <chr> "2199000", "35…
$ Employment <chr> "2112000", "33…
$ Unemployment <chr> "86000", "2400…
$ `Unemployment Rate (%)` <chr> "3.9", "6.6", …
$ `Labor Force Participation Rate, Total (%)` <chr> "56.8", "67.9"…
$ `Labor Force Participation Rate, Male (%)` <chr> "62.1", "72.1"…
$ `Labor Force Participation Rate, Female (%)` <chr> "51.9", "63.3"…
$ `Employment in STEM Occupations (%)` <chr> "5.3", "6.6", …
$ `Doctoral Scientists and Engineers per 100,000 Workers` <chr> "457", "545", …
tail(states_workforce) # shows the last few lines of data
# A tibble: 6 × 10
State `Civilian Labor Force` Employment Unemployment Unemployment Rate (%…¹
<chr> <chr> <chr> <chr> <chr>
1 Wiscons… "3133000" "3039000" "94000" "3"
2 Wyoming "290000" "278000" "12000" "4.1"
3 Source "U.S. Bureau of Labor… "U.S. Bur… "U.S. Burea… "U.S. Bureau of Labor…
4 Date of… "Date of Data: 2018" "Date of … "Date of Da… "Date of Data: 2018"
5 Note <NA> "Number o… "Number of … <NA>
6 Quote "\"Maryland's labor f… "\"Maryla… "\"Maryland… "\"Maryland historica…
# ℹ abbreviated name: ¹`Unemployment Rate (%)`
# ℹ 5 more variables: `Labor Force Participation Rate, Total (%)` <chr>,
# `Labor Force Participation Rate, Male (%)` <chr>,
# `Labor Force Participation Rate, Female (%)` <chr>,
# `Employment in STEM Occupations (%)` <chr>,
# `Doctoral Scientists and Engineers per 100,000 Workers` <chr>
Clean the data
Upon investigation of the data, there are 55 observations, which does not make sense because there should only be 51 (50 states + Washington, DC). After looking at the tail() of the data, the last 4 rows are not useful for working on the data, so they must be removed. I will also be changing the titles of the columns to all lowercase, remove spaces, and remove special characters. All of the columns that should be numeric are chr columns, so they will be changed to dbl or else I can’t make any graphs with the data.
names(states_workforce)
[1] "State"
[2] "Civilian Labor Force"
[3] "Employment"
[4] "Unemployment"
[5] "Unemployment Rate (%)"
[6] "Labor Force Participation Rate, Total (%)"
[7] "Labor Force Participation Rate, Male (%)"
[8] "Labor Force Participation Rate, Female (%)"
[9] "Employment in STEM Occupations (%)"
[10] "Doctoral Scientists and Engineers per 100,000 Workers"
# remove spaces in column names and make all lowercasenames(states_workforce)<-gsub(" ","",names(states_workforce))names(states_workforce)<-tolower(names(states_workforce))# remove special characters (anything that's not the alphabet or numbers) from column namesnames(states_workforce)<-gsub("[^a-zA-Z0-9]", "", names(states_workforce))# change numeric chr columns to dblstates_workforce$employment <-as.numeric(states_workforce$employment)
The employment in STEM data is only shown as a percentage, which is difficult to make a scatterplot with. Therefore, I must calculate the total number of people employed in STEM.
# prepping the y-axis: creating a population number for the STEM employees by multiplying the employed population by the stem occupation percentage.stem_employment<-mutate(states_only, stem_pop = employment * (employmentinstemoccupations/100))stem_employment
# install and load useful packages for aesthetics#install.packages("ggrepel")library(ggrepel)#install.packages("ggthemes")library(ggthemes)
# use ggplot & geom_point() to create a scatterplotstem_employment %>%ggplot(aes(x=employment/10^6,y=stem_pop/10^6, label=state))+geom_point(color="steelblue",size=2)+scale_x_log10()+scale_y_log10()+labs(x="Employed Population (log scale)",y="Population Employed in STEM (log scale)",title="Employment in STEM by State",caption="Maryland Department of Commerce, 2018" )+geom_text_repel(min.segment.length =0, max.overlaps=Inf, size=3)+theme_minimal()
I used this Youtube video, “Creating a labeled scatter plot in R with ggplot2 (CC157)” by Riffomonas Project to learn more about how to use ggrepel so that the labels aren’t overlapping each other too much and so that my data points on the scatterplot aren’t missing any labels of the states.
Bar chart of Laborforce comparison between Male and Female
I would like to compare Maryland, New York, and Florida to see the how the percentages of Males and Females differ in the laborforce.
# filter the tibble to only show male and female laborforce participation rates for Maryland, New York and Floridagender<-states_only[,c("state", "laborforceparticipationratemale", "laborforceparticipationratefemale")]gender2<-filter(gender,state %in%c("New York","Maryland","Florida"))gender2
# A tibble: 3 × 3
state laborforceparticipationratemale laborforceparticipationratefemale
<chr> <dbl> <dbl>
1 Florida 63.5 54.3
2 Maryland 71.9 63.8
3 New York 67.5 58.6
# Bar plot for Male Participationgender2 |>ggplot(aes(x=state,y=laborforceparticipationratemale))+geom_col(aes(fill=state))+scale_y_continuous(limits =c(0, 100))+geom_text(aes(label=laborforceparticipationratemale),vjust=-0.5)+scale_fill_discrete(name="State",labels=c("Florida","Maryland","New York"))+labs(x="State",y="Participation Rate (%)",title="Male Laborforce Participation Rate for Florida, Maryland, and New York",caption="Maryland Department of Commerce, 2018")+theme_classic()
# Bar plot for Female Participationgender2 |>ggplot(aes(x=state,y=laborforceparticipationratefemale))+geom_col(aes(fill=state))+scale_y_continuous(limits =c(0, 100))+geom_text(aes(label=laborforceparticipationratefemale),vjust=-0.5)+scale_fill_discrete(name="State",labels=c("Florida","Maryland","New York"))+labs(x="State",y="Participation Rate (%)",title="Female Laborforce Participation Rate for Florida, Maryland, and New York",caption="Maryland Department of Commerce, 2018")+theme_classic()
When I first looked at the data, I thought it was nice and organized but when I started trying to work with it after importing it into R, I realized that it will be hard to work with because of the special characters, the long column titles, and also because the numbers in the .csv file were imported as chr instead of dbl. I also initially found that there were extra rows that contained text data that was not related to the quantitative columns because they were comments from the State of Maryland that compared it to other states, which were unnecessary for data processing. With this in mind, I used the function gsub() to remove whitespaces and special characters from the column titles so they would be easier to work with. Along the same lines, I used tolower() to make all the column titles lowercase. I then used the as.numeric() function to change the column data types from character to numeric, which initially overlooked. When I first tried making a graph, there was an error because the data was a character data type. The final step I took to clean the data was using slice() to remove the last 4 rows so that only the 50 states and Washington, DC remained.
In my first graph, the scatter plot of the employed population that work in STEM, I initially couldn’t figure out how to create a scatterplot using just the data from the column “Employment in STEM Occupations (%)”. To go around this, added a column by using mutate(), where I used STEM Occupation percentage and multiplied it by the employed population column to get the population in STEM. With this I was able to get the graph “Employment in STEM by State”. I was surprised by the result of the graph because it shows that California and Texas had the highest population of workers in STEM, whereas the tibble shos Washington, D.C., Maryland, and Washington State with the highest percentages in the “Employment in STEM Occupations (%)” column. I may have messed something up along the way that the results were different.
In my second graph, I tried to create a bar graph that would show the male and female working populations for the states of Florida, Maryland, and New York because I thought the differences would be interesting. However, I wasn’t really able to get the visual that I wanted, which was a side-by-side bar chart of the two populations, grouped by state. Overall, the labor force participation rate was much higher for both sexes in Maryland compared to Florida, with New York in between the two. Also the difference in percentage points between Males and Females for Maryland at 8.1 points, versus 9.2 for Florida, and 8.9 for New York. I know that there’s probably a way to combine the two graphs together, but I regrettably couldn’t figure it out. It might have something to do with changing the form of the tibble to a long one.