-
Notifications
You must be signed in to change notification settings - Fork 24
ggplot2
ggplot2 is powerful set of graphing tools that greatly expands both functionally and aesthetically on base R's graphing capabilities. (The 'gg' stands for "grammar of graphics".)
The most comprehensive ggplot2 resources is here. There is a ggplot2 wiki is based here, but this is generally focused on programming related issues.
The wiki page here has some brief information about ggplot2's basic functions to get you started.
To begin making simple plots with ggplot use the qplot (quick plot) command. It is designed to work a lot like R's regular plot command.
Note: The examples on this page use data on Swiss Fertility and Socioeconomic Indicators from 1888. This data is included with R and is stored in a dataframe called
swiss. For more information about the data just type?swissin the Console.
Like with the plot command, to create a simple two-dimensional scatter plot with qplot just type:
qplot(x, y, data = MyData)The x is simply whatever variable you want to be represented by the plot's x-axis and y is whatever variable you want to be along the y-axis. MyData is the data frame.
Here is a basic example with the Education and Examination variables from the swiss data set.
# Load ggplot2
library(ggplot2)
# Create the scatter plot
qplot(Education, Examination, data = swiss)
We use geoms to add other features to our graphs. For a full list of ggplot2 geoms see here.
To add a best fit line with 95% confidence intervals to our scatter plot we can use geom_smooth like this:
qplot(Education, Examination, data = swiss) + geom_smooth()
If you want to use the default smoothing method and confidence interval, you don't need to include anything in the () after geom_smooth. Though you do need to include the (). See the stat_smooth webpage for more details about stat_smooth options.