Use the left and right arrow keys to navigate the presentation forward and backward respectively. You can also use the arrows at the bottom right of the screen to navigate with a mouse.
FAIR USE ACT DISCLAIMER: This site is for educational purposes only. This website may contain copyrighted material, the use of which has not been specifically authorized by the copyright holders. The material is made available on this website as a way to advance teaching, and copyright-protected materials are used to the extent necessary to make this class function in a distance learning environment. The Fair Use Copyright Disclaimer is under section 107 of the Copyright Act of 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, education and research.
require(gapminder)
require(ggplot2)
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
The first thing we do is call the ggplot
function.
This function lets R know that we're creating a new plot, and any of the arguments we give the
ggplot
function are the global options for the plot:
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
We've passed in two arguments to ggplot
.
First, we tell ggplot
what data we want to show on our figure, in this example the gapminder data we read in earlier.
For the second argument, we passed in the aes
function, which tells ggplot
how variables in the data map to aesthetic properties of the figure;
Here we told ggplot
we want to plot the “gdpPercap” column on the x-axis and the “lifeExp” column on the y-axis.
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
Notice that we didn't need to explicitly pass aes
these columns (e.g. x = gapminder[, "gdpPercap"]
);
ggplot
will look in the dataframe for that column.ggplot
isn't enough to draw a figure:ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
We need to tell ggplot
how we want to visually represent the data, which we do by adding a new geom layer.
In our example, we used geom_point
, which tells ggplot
we want to visually represent the relationship between x and y as a scatterplot of points.
Using a scatterplot probably isn't the best for visualizing change over time.
Instead, let's tell ggplot
to visualize the data as a line plot:
ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country, color=continent)) +
geom_line()