Basic Graphics
The purpose of this project is to give you practice with creating different types of plots using the basic R plotting capabilities.
Project tasks
- Open R Studio or a text editor and Create a new R file called project2.R. Put your name, Project 2, and the date at the top of the file. No credit will be given for an R file without your name at the top of the file. Create a project2 directory and save your file and R project there. Set your working directory to the source file directory.
- The General Social Survey is the longest running survey to support social indicator research, with data going back to 1972. Here is the 1991 data, and here is the 2016 data, both in SPSS format. Download the 1991 and 2016 data and save them in your project2 working directory.
- In R Studio, go to the Packages tab and check the foreign package, which lets you read SPSS files. In your project2.R source file, assign the the variable gss91 the result of calling the read.spss function with the filename as the first argument and the additional arguments
to.data.frame=TRUE, use.value.labels = FALSE, trim_values=TRUE. -
After reading the file, have your code print the result of calling
names(gss91). These are the names of all of the columns of data. - Assign to gss16 the result of using read.spss to read the GSS2016.sav file. Except for the filename, use the same arguments as when reading the 1991 file.
-
Using the hist function, Make a histogram of the WRKYEARS field (# of years worked since age 16) of the 1991 data. It's good to assign the result of calling the hist function to a variable to you can access information about the histogram (like the breaks or density values) later on.
- Set the x-axis label and main title to appropriate values.
- Color the bars to something you find pleasing, and have the histogram include a label for each bar. (You may need to set the y limit in order to avoid cropping one of the labels.)
- Print out the histogram breaks and density vector.
- At the end, add the following code to pause execution.
readline( "Press enter to continue" )
(You will have to hit the Enter/Return key in the console to continue execution.) - Make a second histogram of a column of your choice (e.g. AGE works well) and make it look nice. You can find the meaning of each header in the GSS here. After you create your second histogram, add another readline call to pause execution.
- Using the plot function, create a 2-D scatter plot of AGE (x-axis) versus Work Years (y-axis) using the 1991 data. Set an appropriate x-label, y-label, and main title. Modify the color of the points in the plot.
- Make a 2-D scatter plot of two variables of your choice from the 2016 data. Make it look nice.
-
Assign to a variable (e.g. tab91) the result of using the
tablefunction on the CONEDUC field of the 1991 data. Repeat with a second variable (e.g. tab16) using the 2016 data. Note that you can convert the table values to percentages using the following expression.tab91perc <- 100 * tab91 / sum(tab91) tab16perc <- 100 * tab16 / sum(tab16)
-
Using the
barplotfunction, create a plot for the 1991 table for CONEDUC. See if you can set up the category names under the bars in the plot. Give the plot a useful title and y label. Repeat for 2016. Note any differences? - Make two bar plots of a variable of your choice, comparing the 1991 and 2016 data.
Part 1: Setup
Get ready to write some R code and grab some data
Part 2: Read and Explore the Data
Read data from a file and assign it to a data frame, then identify the column names.
Part 3: Histograms
Make some histograms of selected columns.
Part 4: Scatter Plots
Make some scatter plots of selected columns.
Part 5: Bar Plots
Make some bar plots of selected columns.
Note that getting the label names to match up with the numeric may require a little extra work. The value.labels attribute will related these numbers to names, but it may not store them in order from smallest number to largest number. Below is example output:
> print(attr(gss91$CONEDUC,'value.labels'))
HARDLY ANY ONLY SOME A GREAT DEAL
3 2 1
So if I just print the names, I see the same order as above
> print(names(attr(gss91$CONEDUC,'value.labels'))) [1] "HARDLY ANY" "ONLY SOME" "A GREAT DEAL"but that doesn't line up with the order for the values in the contingency table
> print( tab91) 1 2 3 306 557 136Any easy way to fix this is to sort the attributes (which it will do by the numbers) before getting the names
> print(sort(attr(gss91$CONEDUC,'value.labels')))
A GREAT DEAL ONLY SOME HARDLY ANY
1 2 3
> print(names(attr(gss91$CONEDUC,'value.labels')))
[1] "HARDLY ANY" "ONLY SOME" "A GREAT DEAL"
Report
Answer the following questions. Submit your answers as a plain text file or PDF in your handin directory. Put your name at the top of the file. No credit will be given for any other format or for a file without a name.
- What is a histogram?
- What is a 2-D scatter plot?
- What is a bar plot?
- What are two ways you could access the AGE column of the GSS data as a vector if it is stored in a data frame named gss?
- What is an R package?
- If you call the hist() function, how do you store the value it returns into a variable called histdata?
Turn In
- Go to Project 2 in the Google Classroom for CS125
- Add your project2.R and your text/PDF files
- Press the "Turn In" button and then confirm
Spring 2022