data = ...)mapping = aes(...))geom_)scale_)labs(...))facet_)coord_)theme_ et theme())library(gapminder)
dat <- gapminder
ggplot(aes(x = gdpPercap, y = lifeExp), data = dat) +
geom_point() +
scale_x_continuous(labels = scales::dollar_format())Parfois, nous voulons présenter le même graphique, mais pour plusieurs valeurs d’une variable catégorielle.
Possible de garder les couleurs aussi. (Nous allons voir comment retirer la légende dans un instant.)
p2 = ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density() +
facet_wrap(~binary)
p2p3 = ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density() +
facet_wrap(~binary, ncol = 1)
p3p3 = ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density() +
facet_wrap(~binary, nrow = 2)
p3Rappelez-vous: ggplot() fonctionne par étage. Il est donc tout à fait possible d’ajouter les autres étages que nous avons vus précédemment!
p3 = ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density() +
labs(title = "Distributions des notes Metascore en fonction du test Bechdel",
x = "Note Metascore",
y = "Densité") +
facet_wrap(~binary)
p3mutate() et recode(), par exemple)labeller() à l’intérieur de facet_wrap()labeller() à l’intérieur de facet_wrap()Utiliser l’argument labeller() à l’intérieur de facet_wrap()
binary_noms <- c("FAIL" = "Échec",
"PASS" = "Réussite")
p4 = ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density() +
labs(title = "Distributions des notes Metascore en fonction du test Bechdel",
x = "Note Metascore",
y = "Densité") +
facet_wrap(~binary, labeller = as_labeller(binary_noms))
p4La fonction coord_cartesian() permet de faire un zoom sur le graphique.
## [1] "character"
## [1] "numeric"
limits?Quelle est la différence entre coord_cartesian() et limits?
limits supprime les observations qui se trouvent à l’extérieur de l’étendue choisie.coord_cartesian() fait un “zoom”. Les observations sont toujours là, mais on ne les voit plus (elles se trouvent à l’extérieur du cadre).coord_cartesian() vs. limits: ce que ça changecoord_cartesian() vs. limits: ce que ça change# Entre 200 000 000 et 400 000 000$.
p3 = ggplot(data = dat, aes(x = budget_2013, y = domgross_2013))+
geom_point() +
geom_smooth() +
scale_x_continuous(limits = c(200000000,400000000))
p3coord_cartesian() vs. limits: ce que ça change# Entre 200 000 000 et 400 000 000$.
p4 = ggplot(data = dat, aes(x = budget_2013, y = domgross_2013))+
geom_point() +
geom_smooth() +
coord_cartesian(xlim = c(200000000,400000000))
p4coord_flip() permet d’inverser l’axe des x et l’axe des y.
budget sur l’axe des x et intgross sur l’axe des y. Créez une facette pour chaque catégorie de la variable clean_test.budget sur l’axe des x et intgross sur l’axe des y. Créez une facette pour chaque catégorie de la variable clean_test.ggplot(data = dat, aes(x = budget, y = as.numeric(intgross))) +
geom_point() +
facet_wrap(~clean_test)ggplot(data = dat, aes(x = budget, y = as.numeric(intgross))) +
geom_point(alpha = .4) +
geom_smooth() +
facet_wrap(~clean_test) +
coord_cartesian(xlim = c(0,200000000))#install.packages("ggthemes")
library(ggthemes)
ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
theme_tufte() # Edward Tufteggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
theme_economist() # The Economistggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
theme_wsj() # Wall Street JournalRéférences pour les éléments qui se retrouvent dans theme():
axis.title.x(), legend.position(), plot.subtitle(), etc.element_text(), element_rect(), element_line(), element_blank()ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
facet_wrap(~binary) +
theme(axis.title = element_text(hjust = 1))ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
facet_wrap(~binary) +
theme(axis.title = element_text(hjust = 1),
legend.position = "")ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
facet_wrap(~binary)+
labs(title = "Distributions des notes Metascore",
subtitle = "En fonction du résultat au test Bechdel",
x = "Note Metascore",
y = "Densité") +
theme(axis.title = element_text(hjust = 1),
legend.position = "",
plot.title = element_text(size = 18))ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
facet_wrap(~binary)+
labs(title = "Distributions des notes Metascore",
subtitle = "En fonction du résultat au test Bechdel",
x = "Note Metascore",
y = "Densité") +
theme(axis.title = element_text(hjust = 1),
legend.position = "",
plot.title = element_text(size = 18),
panel.background = element_rect(fill = "white"))Et des centaines d’autres combinaisons !
director).year sur l’axe des x et budget_2013 sur l’axe des y. Ajoutez aussi une ligne qui traverse tous les points.clean_test.director).year sur l’axe des x et budget_2013 sur l’axe des y. Ajoutez aussi une ligne qui traverse tous les points.clean_test.ggplot(data = dat_court, aes(x = year, y = budget_2013)) +
geom_point(size = 2, aes(color = clean_test)) +
geom_line()ggplot(data = dat_court, aes(x = year, y = budget_2013)) +
geom_point(size = 2, aes(color = clean_test)) +
geom_line() +
scale_y_continuous(labels = scales::dollar_format()) +
scale_color_discrete(name = "Test Bechdel",
labels = c("Résultat incertain", "Parlent à des hommes",
"Ne parlent pas", "Pas de femmes",
"Réussite")) +
labs(title = "Budget des films de S. Spielberg et résultat du test Bechdel",
x = "Année",
y = "Budget (dollars de 2013)") +
theme_minimal()theme().ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
facet_wrap(~binary)+
labs(title = "Distributions des notes Metascore",
subtitle = "En fonction du résultat au test Bechdel",
x = "Note Metascore",
y = "Densité") +
theme(plot.title = element_text(family = "Times New Roman"))extrafont permet de voir quelles polices de caractère sont disponibles sur votre ordinateur et de les importer dans R.ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
facet_wrap(~binary)+
labs(title = "Distributions des notes Metascore",
subtitle = "En fonction du résultat au test Bechdel",
x = "Note Metascore",
y = "Densité") +
theme(plot.title = element_text(family = "Futura-Bold", size = 18))font_import() pour importer toutes les policies qui se trouvent sur votre ordinateur dans R.fonts().La fonction ggsave() permet d’enregistrer vos graphiques sur votre ordinateur (.png, .pdf, etc.).
?ggsave
p = ggplot(data = dat, aes(x = metascore, fill = binary)) +
geom_density(alpha = .4) +
scale_fill_discrete(name = "",
labels = c("Échec","Réussite")) +
labs(title = "Distributions des notes Metascore",
subtitle = "En fonction du résultat au test Bechdel",
x = "Note Metascore",
y = "Densité") +
theme_minimal() +
theme(axis.title = element_text(hjust = 1),
legend.position = "",
plot.title = element_text(size = 16))
ggsave("demo_enregistrer.png", plot = p, width = 8, height = 5)ggsave(), le graphique se trouvera au même emplacement que le fichier à partir duquel vous travaillez puisque vous vous trouvez à l’intérieur d’un R Project.setwd(). Exemple: