Your First Chart in R SOCL 3633 Research Methods · Lab 1 · Midwestern State University

Today you make a chart from real survey data, change it, and then make a chart of your own choosing. If you have never written code, that is expected. Every step below says what to click and what you should see. Keep this page open next to RStudio and work through the tabs in order. Before you leave, you submit two things in the Lab 1 submission on D2L: your response sheet and the picture file of your own chart.

Get set up

Check off each step as you finish it.

The four parts of the RStudio window

Top left · Script
The file you just opened. You read and change the code here.
Bottom left · Console
Where R prints messages and results when the code runs.
Top right · Environment
A list of the things R is holding, such as the data and your chart.
Bottom right · Files and Plots
The Plots tab shows your chart. The Files tab shows the files in your folder.

Run the script

The script uses the General Social Survey (GSS), which has interviewed national samples of American adults since 1972. This version covers 2000 to 2014 and includes 21,483 people. The script asks one question: what share of American adults report no religion, and how did that share change?

Before you run it: in 2014, what percent of American adults in the GSS reported no religion?
10%

Now run it

  1. Click anywhere inside the script.
  2. Click the Source button at the top right of the script pane.
  3. Wait a few seconds. The chart appears in the Plots tab, lower right.
Checkpoint. Your chart should look like the one below, with the title "Religious non-affiliation rose in the United States." If it does not, look at the table of common problems further down, or ask the person next to you.
Line chart: the share of GSS respondents reporting no religion rises from about 14 percent in 2000 to about 21 percent in 2014.

What the script just did

  1. Step 1 loaded three packages. A package is a set of tools that other people wrote. One of them contains the GSS data.
  2. Step 2 showed you the data in the Console. Each row is one person, and each column is one survey question, called a variable.
  3. Step 3 calculated, for each survey year, the share of people whose answer to the religion question was "None," and drew those shares as a line.

If something goes wrong

What you seeWhat to do
"there is no package called"The computer is missing a package. Step 0 of the script installs it; wait for it to finish, then click Source again. If it still fails, type install.packages("ggplot2") in the Console, using the package name from the message, and press Enter.
"could not find function"Click Source again so the script runs from the top. The library() lines have to run first.
"object not found"Check spelling and capital letters. R treats Gss and gss as different names.
A + sign in the Console and nothing happensA quotation mark or parenthesis is missing. Click in the Console, press Escape, fix the line, and click Source again.
No chart appearsMake sure the line print(my_chart) is still in the script.

Change it: a guided step

Code is something you can change. In this step you change one word and the labels, and the script makes a different chart.

  1. In Step 3 of the script, find this line:
    summarize(share = mean(relig == "None"))
  2. Change "None" to "Protestant". Keep the quotation marks. The line should now read:
    summarize(share = mean(relig == "Protestant"))
  3. Change the title, the subtitle, and the y-axis label (y = ) so that they describe the new chart. Keep the quotation marks around each label.
  4. Click Source again.
Checkpoint. Your new line should fall, as in the chart below. Your title will use your own words. Check that your title and labels say Protestant, not "no religion."
Line chart: the share of GSS respondents reporting Protestant falls from about 54 percent in 2000 to about 44 percent in 2014.
One group's share rose and another's fell. Keep this in mind for question 3 on the response sheet: does this chart show you that individual people left one religion for another?

On your own

This step has no example to copy. You decide what to chart, write your own labels, and write one line of code yourself.

  1. Pick a group. When you clicked Source, the Console printed a list of every religious group in the data and the number of people in each. Pick any group other than None or Protestant. Change the group name in Step 3 to your group. Type it exactly as it appears in the list, with the same capital letters, inside quotation marks.
  2. Write your own labels. Change the title, subtitle, and y-axis label to describe your chart.
  3. Save your chart as a picture. At the bottom of the script, under the comment "Write your ggsave() line here," write one line that saves your chart. Use the ggsave() example on your R reference sheet. Name the file with your last name, for example smith_chart.png.
  4. Click Source. Open the Files tab in the lower right. Your picture file should be there.
Two notes. A group with few people in the survey gives a jagged line that jumps up and down from year to year. That comes from small numbers, not necessarily from real change. Also, some group names in this 2000 to 2014 data use older spellings. Type each name exactly as it appears in the list, because R matches the text letter for letter.
Before you leave: save the script in your SOCL3633 folder (File, then Save). You will submit the picture file with your response sheet.

Lab 1 response sheet

Answer the four questions in complete sentences. Then assemble your answers into one block of text and paste it into the Lab 1 submission in D2L. In the same submission, upload the picture file of your own chart. Your answers stay on this page and are not sent anywhere until you paste them.

Your name
1. Describe what the first chart (no religion) shows. Use the percentages for 2000 and 2014.
One to two sentences.
2. Which group did you choose on your own, and what happened to its share between 2000 and 2014?
One to two sentences.
3. One group's share rose while another's fell. Does the chart show individual people leaving one religion for another? Explain why it can or cannot.
Two to four sentences.
4. Which step today was hardest for you, and what did you do to get past it?
One to two sentences.

Sources

NORC at the University of Chicago. General Social Survey, 2000 to 2014. Accessed through the gss_cat extract in the forcats package for R. https://gss.norc.org
R Core Team. 2024. R: A Language and Environment for Statistical Computing. Vienna: R Foundation for Statistical Computing. https://www.R-project.org
Wickham, Hadley. 2016. ggplot2: Elegant Graphics for Data Analysis. New York: Springer.
Wickham, Hadley. 2023. forcats: Tools for Working with Categorical Variables (Factors). R package version 1.0.0. https://forcats.tidyverse.org
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. dplyr: A Grammar of Data Manipulation. R package. https://dplyr.tidyverse.org

The percentages on this page are unweighted shares of the people in this GSS extract. Published GSS figures use survey weights and can differ slightly.