author: Diogo Melo date: 2019/10/29 font-family: ‘Helvetica’ width: 1366 height: 768
left: 35%
left: 35%
“ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics.”
head(iris, 10)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
library(tidyr)
head(
gather(iris, trait, value, Sepal.Length:Petal.Width),
10)
Species trait value
1 setosa Sepal.Length 5.1
2 setosa Sepal.Length 4.9
3 setosa Sepal.Length 4.7
4 setosa Sepal.Length 4.6
5 setosa Sepal.Length 5.0
6 setosa Sepal.Length 5.4
7 setosa Sepal.Length 4.6
8 setosa Sepal.Length 5.0
9 setosa Sepal.Length 4.4
10 setosa Sepal.Length 4.9
Regra geral:
ggplot(data_frame_entrada, aes(x = coluna_eixo_x,
y = coluna_eixo_y,
group = coluna_agrupadora,
color = coluna_das_cores))
+ geom_tipo_do_grafico(opcoes que não dependem dos dados,
aes(opcoes que dependem))
ggplot(data = iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
ggplot(data = iris, aes(Sepal.Length, Sepal.Width,
color = Species)) +
geom_point()
ggplot(diamonds, aes(price)) + geom_histogram(bins = 500)
ggplot(data = iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(color = Species)) + facet_wrap(~Species)
ggplot(data = iris, aes(Sepal.Length, Sepal.Width,
color = Species)) +
geom_point() + geom_smooth()
ggplot(data = iris, aes(Sepal.Length, Sepal.Width,
color = Species)) +
geom_point() + geom_smooth(method = "lm")
library(tidyr)
narrow_iris = pivot_longer(iris, -Species)
ggplot(narrow_iris, aes(Species, value)) +
geom_boxplot() + geom_jitter(width = 0.2, height = 0) + facet_wrap(~name, scales="free")
library(tidyr)
narrow_iris = pivot_longer(iris, -Species)
ggplot(narrow_iris, aes(Species, value)) +
geom_boxplot() + geom_dotplot(binaxis = 'y',
dotsize = 0.5,
stackdir = 'center') + facet_wrap(~name, scales="free")
left: 60%
No ggplot os objetos gráficos podem ser manipulados ou armazenados, diferente dos plots padrão onde os gráficos são “efeitos colaterais”
Isso significa que vc pode alterar gráficos já feitos usando “+”
library(gapminder)
meu_grafico = ggplot(gapminder, aes(x = log(gdpPercap), y = log(lifeExp))) + geom_point(aes(color = continent))
meu_grafico
left: 60%
Algumas opções comuns:
meu_grafico = meu_grafico +
labs(x = "GDP per capta",
y = "Expectativa de vida")
meu_grafico = meu_grafico + theme(text = element_text(size = 30), legend.title = element_text(face = "italic")) + scale_color_discrete(name = "Continente")
meu_grafico
left: 30%
Temas prontos!
library(cowplot)
meu_grafico = meu_grafico + theme_cowplot()
meu_grafico
Temas prontos!
library(ggthemes)
meu_grafico = ggplot(gapminder,
aes(x = log(gdpPercap),
y = log(lifeExp))) +
geom_point(size = 3,
aes(shape = continent)) +
theme_wsj()
meu_grafico
Documentação