View Interactive Version
ggplot(___) + geom_point( mapping = aes(x = ___, y = ___, color = ___, size = ___), alpha = ___ )
Typically, the point color is used to introduce a new dimension to a scatter plot. In ggplot we use the
color aesthetic to specify the mapping of a variable to the color of the points.
gapminder_2007 dataset we can plot the GDP per capita
gdpPercap vs. the life expectancy
lifeExp as follows:
ggplot(gapminder_2007) + geom_point(aes(x = gdpPercap, y = lifeExp))
To color each point based on the
continent of each country we can use:
ggplot(gapminder_2007) + geom_point(aes(x = gdpPercap, y = lifeExp, color = continent))
We see that in the resulting plot each point is colored differently based on the
continent of each country.
ggplot uses the coloring scheme based on the categorical data type of the variable
By contrast, let’s see how the plot looks like if we color the points by the
numeric variable population
ggplot(gapminder_2007) + geom_point(aes(x = gdpPercap, y = lifeExp, color = pop))
The scale immediately changes to continuous as it can be seen in the legend and the light-blue points are now the countries with the highest population number (China and India).