library(rgl)
library(devout)
library(devoutrgl)
library(ggrgl)
library(ggplot2)
library(dplyr)
library(gapminder)
geom_line_3d()
The {gapminder} dataset contains "data on life expectancy, GDP per capita, and population by country.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GapMinder dataset
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set.seed(1)
gapminder_df <- gapminder::gapminder %>%
filter(country %in% sample(unique(.$country), 6)) %>%
mutate(country = as.character(country))
last_rows <- gapminder_df %>% filter(year == 2007)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create plot with `geom_line_3d()`
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p <- ggplot(gapminder_df, aes(x=year, y=lifeExp, z=gdpPercap, group = country)) +
geom_line_3d(aes(colour = country)) +
geom_text_z(data = last_rows, aes(label = country)) +
labs(
title = "'gapminder' country data. Height indicates GDP-per-capita",
subtitle = "ggrgl::geom_line_3d() with {devoutrgl}"
) +
theme_ggrgl() +
theme(legend.position = 'none')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev(fov = 30, view_angle = -30)
p
invisible(dev.off())
Use your mouse, mouse buttons and scrollwheel to manipulate the 3d plot