library(tidyverse)
library(gapminder)
ggsave for ggplot
library(here)
## here() starts at /Users/dianalin/STAT545-participation
(p <- ggplot(mtcars, aes(hp, wt)) +
geom_point())
ggsave(here("cm013","mtcars.png"), p)
## Saving 7 x 5 in image
pdf()/jpeg()/png()… and dev.off().n by m grid of pixels, each with its own colour. jpeg, png, gif, bmp.pdf, svg.Scale functions in ggplot2 take the form scale_[aesthetic]_[mapping]().
Let’s first focus on the following plot:
p_scales <- ggplot(gapminder, aes(gdpPercap, lifeExp)) +
geom_point(aes(colour=pop), alpha=0.2)
p_scales +
scale_x_log10() +
scale_colour_continuous(trans="log10")
p_scales +
scale_x_log10() +
scale_colour_continuous(
trans = "log10",
breaks = 10^(1:10)
) +
scale_y_continuous(breaks=10*(1:10))
scales::*_format in the labels argument of a scale function to do the following:
scales::dollar_format())scales::comma_format())library(scales)
p_scales +
scale_x_log10(labels=scales::dollar_format()) +
scale_colour_continuous(
trans = "log10",
breaks = 10^(1:10),
labels = scales::comma_format()
) +
scale_y_continuous(breaks=10*(1:10))
RColorBrewer to change the colour scheme.
## All palettes the come with RColorBrewer:
RColorBrewer::display.brewer.all()
p_scales +
scale_x_log10(labels=dollar_format()) +
scale_colour_distiller(
trans = "log10",
breaks = 10^(1:10),
labels = comma_format(),
palette = "Purples"
) +
scale_y_continuous(breaks=10*(1:10))
viridis to change the colour to a colour-blind friendly scheme
scale_colour_viridis_c (c stands for continuous; d discrete).option.p_scales +
scale_x_log10(labels=dollar_format()) +
scale_colour_viridis_c(
trans = "log10",
breaks = 10^(1:10),
labels = comma_format(),
option = "E"
) +
scale_y_continuous(breaks=10*(1:10))
Changing the look of a graphic can be achieved through the theme() layer.
There are “complete themes” that come with ggplot2, my favourite being theme_bw (I’ve grown tired of the default gray background, so theme_bw is refreshing).
theme_bw():ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
facet_wrap(~ Species) +
geom_point() +
labs(x = "Sepal Width",
y = "Sepal Length",
title = "Sepal sizes of three plant species")
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
facet_wrap(~ Species) +
geom_point() +
labs(x = "Sepal Width",
y = "Sepal Length",
title = "Sepal sizes of three plant species") +
theme_bw() +
theme(axis.title = element_text(size = 28),
strip.background = element_rect())
Consider the following plot:
(p <- gapminder %>%
filter(continent != "Oceania") %>%
ggplot(aes(gdpPercap, lifeExp)) +
geom_point(aes(colour=pop), alpha=0.2) +
scale_x_log10(labels=dollar_format()) +
scale_colour_distiller(
trans = "log10",
breaks = 10^(1:10),
labels = comma_format(),
palette = "Greens"
) +
facet_wrap(~ continent) +
scale_y_continuous(breaks=10*(1:10)) +
theme_bw())
plotly object by applying the ggplotly() function:library(plotly)
p %>%
ggplotly()
p %>%
ggplotly() %>%
htmlwidgets::saveWidget("~/STAT545-participation/cm013/plotly-widget.html")
library(listviewer)
p %>%
ggplotly() %>%
plotly_json(jsonedit = TRUE)
plot_ly() – scatterplot of gdpPercap vs lifeExp.
plot_ly(gapminder,
x = ~gdpPercap,
y = ~lifeExp,
type = "scatter",
mode = "markers",
opacity = 0.2) %>%
layout(xaxis = list(type = "log"))
plot_ly(gapminder,
x = ~gdpPercap,
y = ~lifeExp,
z = ~pop,
type = "scatter3d",
mode = "markers",
opacity = 0.2)