Skip to content

Instantly share code, notes, and snippets.

@naomispence
Created March 6, 2017 19:49
Show Gist options
  • Save naomispence/ef71c0342f1e12dc7eadc84b6c11dc40 to your computer and use it in GitHub Desktop.
Save naomispence/ef71c0342f1e12dc7eadc84b6c11dc40 to your computer and use it in GitHub Desktop.
TO DO A GRAPH IN R STUDIO, YOU FIRST NEED TO ADD A NEW LIBRARY USING THE LINE OF CODE BELOW.
library('ggplot2')
(YOU SHOULD PASTE THIS LINE OF CODE INTO CHUNK 1 ON IT'S OWN LINE NEAR THE OTHER LINES OF CODE THAT START WITH library)
(DON'T FORGET TO RUN CHUNK 1 AFTER PASTING SO THAT IT LOADS THE NEW LIBRARY FOR YOU.)
AFTER YOU'VE ADDED THE LIBRARY ggplot2, YOU CAN BEGIN TO USE THE COMMAND ggplot TO CREATE GRAPHS.
ALWAYS CONSIDER WHICH GRAPH IS APPROPRIATE FOR YOUR VARIABLE BASED ON LEVEL OF MEASUREMENT.
ALSO CONSIDER WHICH GRAPH WILL DISPLAY THE INFORMATION CLEARLY GIVEN THE VARIABLE'S VALUES.
BE SURE TO ALWAYS HAVE AXIS LABELS AND TITLES ON YOUR GRAPH THAT ARE CLEAR, ACCURATE, AND DESCRIBE THE GRAPH.
HERE IS AN EXAMPLE FOR A BAR GRAPH FOR THE VARIABLE NAMED RACE:
```{r}
#THE LINE OF CODE BELOW GIVES YOU A "BARE BONES" BAR GRAPH
ggplot(data = GSS, aes(x = race)) + geom_bar()
#THE LINE OF CODE BELOW ADDS MANY GRAPHING OPTIONS TO IMPROVE THE GRAPH
#NOTE THAT THE + AT THE END OF THE LINE TELLS R TO CONTINUE READING ON THE NEXT LINE
ggplot(GSS, aes(race)) + geom_bar(color="red", fill="white", aes(y = ((..count..)/sum(..count..)))) +
scale_y_continuous(labels = scales::percent) +
ggtitle("Figure 1. Bar Graph of the Distribution of Adult Americans by Race") +
labs(y="Percent", x="Race")
```
HERE IS AN EXAMPLE FOR A HISTOGRAM FOR THE VARIABLE NAMED CHILDS:
```{r}
ggplot(data = GSS, aes(x = childs)) + geom_histogram(color="blue", fill="pink", binwidth =1, aes(y=(..count../sum(..count..))*100)) +
ggtitle("Figure 3. Distribution of Adult Americans by Number of Children Ever Born") +
labs(y="Percent", x="Number of Children")
```
THINK ABOUT WHICH PIECES OF THE CODE YOU CAN/SHOULD REPLACE TO GENERATE GRAPHS FOR YOUR OWN VARIABLES:
VARIABLE NAME (find and replace),
COLORS (outline colors and or fill colors; type the same color name if you want solid color),
TITLE (edit inside of double quotation marks; use Title Case, proper spelling, etc.; be clear, accurate, and describe the graph),
LABELS (called labs; edit inside of the double quotation marks; use Title Case, proper spelling, etc.; be clear, accurate)
BINWIDTH for histograms (described in swirl lesson; often, you want to use binwidth=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment