Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
ggplot2:	An	extensible	platform	
for	publication-quality	graphics
Claus	O.	Wilke
The	University	of	Texas	at	Austin
@clauswilke clauswilke
Hadley	Wickham Thomas	Lin	Pederson
Edzer Pebesma Kamil Slowikowski
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
serialmentor.com/dataviz
What	do	we	need?
• Powerful	styling	options
• Broad	selection	of	plot	types
• Sophisticated	text	annotations
• Plot	composition
What	do	we	need?
• Powerful	styling	options
• Broad	selection	of	plot	types
• Sophisticated	text	annotations
• Plot	composition
ggplot2:	A	grammar	of	graphics	in	R
ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
geom_point()
ggplot2:	A	grammar	of	graphics	in	R
ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
geom_point() +
scale_color_viridis_d()
ggplot2:	A	grammar	of	graphics	in	R
ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
geom_point() +
scale_color_viridis_d() +
theme_bw()
What	do	we	need?
• Powerful	styling	options
• Broad	selection	of	plot	types
• Sophisticated	text	annotations
• Plot	composition
ggplot2-exts.org
Example	1:
Ridgeline	plots
Ridgeline	plots	are	a	variation	of	density	plots
ggplot(iris) +
aes(x = Sepal.Length, fill = Species) +
geom_density(alpha = 0.5)
Ridgeline	plots	are	a	variation	of	density	plots
library(ggridges)
ggplot(iris) +
aes(x = Sepal.Length, y = Species) +
geom_density_ridges(alpha = 0.5)
Ridgeline	plots	are	a	variation	of	density	plots
library(ggridges)
ggplot(iris) +
aes(x = Sepal.Length, y = Species) +
geom_density_ridges(alpha = 0.5)
Example	2:
Simple	features
sf:	Manipulating	and	plotting	simple	features
ggplot(TX_income, aes(fill = median_income)) +
geom_sf()
Edzer
Pebesma
TX_income[1:3,]
#> Simple feature collection with 3 features and 15 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -579099 ymin: -958812.5 xmax: -168152.2 ymax: -94284.87
#> epsg (SRID): NA
#> proj4string: +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96
+x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> STATEFP COUNTYFP COUNTYNS AFFGEOID GEOID NAME LSAD ALAND
#> 1 48 421 01383996 0500000US48421 48421 Sherman 06 2390651189
#> 2 48 493 01384032 0500000US48493 48493 Wilson 06 2081662847
#> 3 48 115 01383843 0500000US48115 48115 Dawson 06 2331781556
#> AWATER name median_income median_income_moe
#> 1 428754 Sherman County, Texas 51987 4386
#> 2 12111367 Wilson County, Texas 68100 3328
#> 3 4720730 Dawson County, Texas 41095 2591
#> population area popdens
#> 1 3066 2387929738 [m^2] 1.283957e-06 [1/m^2]
#> 2 45509 2086919506 [m^2] 2.180678e-05 [1/m^2]
#> 3 13542 2336898468 [m^2] 5.794860e-06 [1/m^2]
#> geometry
#> 1 MULTIPOLYGON (((-546533.1 -...
#> 2 MULTIPOLYGON (((-234487.5 -...
#> 3 MULTIPOLYGON (((-576468.3 -...
sf:	Manipulating	and	plotting	simple	features
ggplot(TX_income, aes(fill = median_income)) +
geom_sf() +
coord_sf(crs = 4326) # Cartesian longitude and latitude
Edzer
Pebesma
sf:	Manipulating	and	plotting	simple	features
ggplot(TX_income, aes(fill = median_income)) +
geom_sf() +
coord_sf(crs = 3338) # NAD83, Alaska Albers
Edzer
Pebesma
sf:	Manipulating	and	plotting	simple	features
# with formatting tweaks
ggplot(TX_income, aes(fill = median_income)) +
geom_sf(color = "black", size = 0.2) +
scale_fill_viridis_c(
name = "income",
labels = dollar
) +
theme_minimal()
Edzer
Pebesma
What	do	we	need?
• Powerful	styling	options
• Broad	selection	of	plot	types
• Sophisticated	text	annotations
• Plot	composition
ggplot2: An Extensible Platform for Publication-quality Graphics
ggrepel:	Smart	annotation	of	plot	elements
library(ggrepel)
mtcars$car <- rownames(mtcars) # add column of car names
mtcars %>% filter(disp < 200) %>%
ggplot(aes(disp, mpg, label = car)) +
geom_point() +
geom_text_repel()
Kamil Slowikowski
ggrepel:	Smart	annotation	of	plot	elements
library(ggrepel)
mtcars$car <- rownames(mtcars)
mtcars %>% filter(disp < 200) %>%
ggplot(aes(disp, mpg, label = car)) +
geom_point() +
geom_text_repel()
Kamil Slowikowski
ggrepel:	Smart	annotation	of	plot	elements
mtcars %>% filter(disp < 200) %>%
ggplot(aes(disp, mpg, label = car)) +
geom_point() +
geom_text_repel(xlim = c(180, 250), hjust = 0) +
xlim(70, 240)
Kamil Slowikowski
ggrepel:	Smart	annotation	of	plot	elements
mtcars %>% filter(disp < 200) %>%
ggplot(aes(disp, mpg, label = car)) +
geom_point() +
geom_text_repel(xlim = c(180, 250), hjust = 0) +
xlim(70, 240)
Kamil Slowikowski
ggrepel:	Smart	annotation	of	plot	elements
mtcars %>% filter(disp < 200) %>%
ggplot(aes(disp, mpg)) +
geom_point(aes(color = factor(am))) +
geom_text_repel(aes(label = ifelse(am == 0, car, ""))) +
scale_color_manual(
name = "transmission",
labels = c("manual", "automatic"),
values = c("red", "gray50")
)
Kamil Slowikowski
ggrepel:	Smart	annotation	of	plot	elements
mtcars %>% filter(disp < 200) %>%
ggplot(aes(disp, mpg)) +
geom_point(aes(color = factor(am))) +
geom_text_repel(aes(label = ifelse(am == 0, car, ""))) +
scale_color_manual(
name = "transmission",
labels = c("manual", "automatic"),
values = c("red", "gray50")
)
Kamil Slowikowski
What	do	we	need?
• Powerful	styling	options
• Broad	selection	of	plot	types
• Sophisticated	text	annotations
• Plot	composition
ggplot2: An Extensible Platform for Publication-quality Graphics
patchwork:	A	grammar	of	plot	composition	
p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
p2 <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# add plots to place them side-by-side
p1 + p2
Thomas	Lin	
Pederson
Thomas	Lin	
Pederson
patchwork:	A	grammar	of	plot	composition	
p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
p2 <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# add plots to place them side-by-side
p1 + p2
patchwork:	A	grammar	of	plot	composition	
p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
p2 <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# divide plots to place them on top of each other
p1 / p2
Thomas	Lin	
Pederson
Thomas	Lin	
Pederson
patchwork:	A	grammar	of	plot	composition	
p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
p2 <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# divide plots to place them on top of each other
p1 / p2
patchwork:	A	grammar	of	plot	composition	
p3 <- ggplot(iris, aes(Sepal.Length)) +
geom_density(fill = "gray60") +
facet_wrap(~Species)
# nested arrangements are possible
p3 / (p1 + p2)
Thomas	Lin	
Pederson
Thomas	Lin	
Pederson
patchwork:	A	grammar	of	plot	composition	
p3 <- ggplot(iris, aes(Sepal.Length)) +
geom_density(fill = "gray60") +
facet_wrap(~Species)
# nested arrangements are possible
p3 / (p1 + p2)
patchwork:	A	grammar	of	plot	composition	
p3 <- ggplot(iris, aes(Sepal.Length)) +
geom_density(fill = "gray60") +
facet_wrap(~Species)
# apply theme recursively to all plots
p3 / (p1 + p2) & theme_minimal()
Thomas	Lin	
Pederson
Thomas	Lin	
Pederson
patchwork:	A	grammar	of	plot	composition	
p3 <- ggplot(iris, aes(Sepal.Length)) +
geom_density(fill = "gray60") +
facet_wrap(~Species)
# apply theme recursively to all plots
p3 / (p1 + p2) & theme_minimal()
Read	the	source,	Luke
Acknowledgments
ggplot2	team
Hadley	Wickham
Winston	Chang
Lionel	Henry
Thomas	Lin	Pedersen
Kohske Takahashi
Hiroaki	Yutani
Kara	Woo
Specific	package	authors
Edzer Pebesma (sf)
Kamil Slowikowski (ggrepel)
Thomas	Lin	Pedersen	(patchwork)
The	broader	R	community
Mara	Averick
Yihui Xie
RStudio
many	others
Claus	O.	Wilke	receives	funding	from:	UT	Austin,	R	Consortium,	NIH,	NSF

More Related Content

ggplot2: An Extensible Platform for Publication-quality Graphics