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
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?
Now run it
- Click anywhere inside the script.
- Click the Source button at the top right of the script pane.
- Wait a few seconds. The chart appears in the Plots tab, lower right.
What the script just did
- Step 1 loaded three packages. A package is a set of tools that other people wrote. One of them contains the GSS data.
- Step 2 showed you the data in the Console. Each row is one person, and each column is one survey question, called a variable.
- 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 see | What 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 happens | A quotation mark or parenthesis is missing. Click in the Console, press Escape, fix the line, and click Source again. |
| No chart appears | Make 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.
- In Step 3 of the script, find this line:
summarize(share = mean(relig == "None"))
- Change
"None"to"Protestant". Keep the quotation marks. The line should now read:summarize(share = mean(relig == "Protestant"))
- Change the
title, thesubtitle, and the y-axis label (y =) so that they describe the new chart. Keep the quotation marks around each label. - Click Source again.
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.
- 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.
- Write your own labels. Change the title, subtitle, and y-axis label to describe your chart.
- 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. - Click Source. Open the Files tab in the lower right. Your picture file should be there.
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.
Sources
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.