Skip to Main Content

R Studio Basics for Data Visualization: Use the Data for Visualizations

For the R Studio Data Visualizaton Workshops, November 2018

Cheat Sheet for ggplot

Basics

The basic structure of a plot command looks like this:

ggplot(data = {dataset name}) + {geom_function}(mapping = aes(<mappings>))

Make it fancy:

Here's an example where we set the color to a variable:

ggplot(data = Over18) + geom_point(mapping = aes (x=MARHYP, y=AGEP, color=SEX))

Here's another example if we just want the whole plot to be a certain color (not set to a variable, just to make it pretty!):

ggplot(data = {datasetname}) + {geom_function}(mapping = aes (x=BLAH, y=NAH), color = "blue")

Some geoms to get you started:

geom_point  Makes a scatterplot. Add "jitter" to make each point more noticeable if there are many overlapping points. Example:

ggplot(data=adultfemales) + geom_point(mapping=aes(x=MARHYP, y=AGEP), position = "jitter", color = "green")

geom_bar Counts the number of times something is listed in your dataset and displays the counts in bars. If you want to set a y axis variable other than the count (if the count is already present in the data), you need to add stat="identity" like this:

ggplot(data = TopDogs) + geom_bar (mapping = aes (x=DogName, y = Count), stat = "identity")

geom_smooth Makes a smoothed line fitted to the data (does not hit all points of data).

 

 

Find more geoms on the ggplot2 Cheat Sheet here: RStudio ggplot Cheat Sheet

Hint: You can always type ??{geom_name} to learn how a specific geom works and see examples of it.