This is a demonstration showing how to make your first plot using R, Rstudio, and ggplot.
Steps:
tidyverse
and
gapminder
libraries (and that they are installed on your
computer).gapminder
and a mapping
between data and aesthetic features. For example put GDP per capita on
the x axis and life expectancy on the y axis.geom_point
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + geom_point()
ggplot(gapminder, aes(y = gdpPercap, x = lifeExp)) + geom_point()
Refine your plot:
labs
and the arguments
x
, y
, title
,
subtitle
, caption
, tag
(some of
these are optional and not always needed.)theme
to make the text bigger by setting the
size
property of text
with the
element_text
function.scale_x_log10
ggplot(gapminder, aes(y = gdpPercap,
x = lifeExp)) +
geom_point() +
labs(x = "GDP per capita (US$, inflation adjusted",
y = "Life expectancy (years, at birth)",
title = "Life expectancy varies with national income",
subtitle = "Something more here",
caption = "Source: gapminder")
ggplot(gapminder, aes(y = gdpPercap,
x = lifeExp,
color = continent),
) +
geom_point() +
labs(y = "GDP per capita (US$, inflation adjusted",
x = "Life expectancy (years, at birth)",
title = "Life expectancy varies with national income",
subtitle = "Something more here",
caption = "Source: gapminder")
ggplot(gapminder, aes(y = gdpPercap,
x = lifeExp,
color = continent),
) +
geom_point() +
labs(y = "GDP per capita (US$, inflation adjusted",
x = "Life expectancy (years, at birth)",
title = "Life expectancy varies with national income",
subtitle = "Something more here",
caption = "Source: gapminder") +
scale_y_log10() +
theme(text = element_text(size=14))
Repeat:
mtcars
or
palmerpenguins
.Refer to the slides for this lesson for more ideas and examples.