Advanced ggplot

Lokesh Mano

NBIS, SciLifeLab

14-Apr-2025

Contents

Axis

  • scales: x, y
  • syntax: scale_<axis>_<type>
  • arguments: name, limits, breaks, labels

Axis β€’ Scales

Coordinate Systems

  • coord_cartesian(xlim=c(2,8)) for zooming in
  • coord_map for controlling limits on maps
  • coord_polar

Coordinate Systems

Stats

  • Stats compute new variables from input data.
  • Geoms have default stats.
x <- ggplot(penguins) + geom_bar(aes(x=flipper_length_mm),stat="bin")
y <- ggplot(penguins) + geom_bar(aes(x=species),stat="count")
z <- ggplot(penguins) + geom_bar(aes(x=species,y=flipper_length_mm),stat="identity")
wrap_plots(x,y,z,nrow=1)

Stats

  • Plots can be built with stats.
x <- ggplot(penguins) + stat_bin(aes(x=flipper_length_mm),geom="bar")
y <- ggplot(penguins) + stat_count(aes(x=species),geom="bar")
z <- ggplot(penguins) + stat_identity(aes(x=species,y=flipper_length_mm),geom="bar")
wrap_plots(x,y,z,nrow=1)

Stats

  • Stats have default geoms.
plot stat geom
histogram bin bar
smooth smooth line
boxplot boxplot boxplot
density density line
freqpoly freqpoly line

Use args(geom_bar) to check arguments.

Stats

  • stat_compare_means() from the package ggpubr.
ggplot(penguins, aes(x=species,y=flipper_length_mm, fill=species)) + 
  geom_boxplot() + 
  ggpubr::stat_compare_means()

Stats

  • stat_compare_means() from the package ggpubr.
my_comparisons <- list( c("Adelie", "Chinstrap"), c("Chinstrap", "Gentoo"), c("Adelie", "Gentoo") )
ggplot(penguins, aes(x=species,y=flipper_length_mm, fill=species)) + 
  geom_boxplot() + 
  ggpubr::stat_compare_means(comparisons = my_comparisons, method = "t.test")

Theme

  • Modify non-data plot elements/appearance
  • Axis labels, panel colors, legend appearance etc
  • Save a particular appearance for reuse
  • ?theme

Theme

Theme β€’ Text

element_text(family=NULL,face=NULL,color=NULL,size=NULL,hjust=NULL,
             vjust=NULL, angle=NULL,lineheight=NULL,margin = NULL)
p <- p + theme(
    axis.title=element_text(color="#e41a1c"),
    axis.text=element_text(color="#377eb8"),
    plot.title=element_text(color="#4daf4a"),
    plot.subtitle=element_text(color="#984ea3"),
    legend.text=element_text(color="#ff7f00"),
    legend.title=element_text(color="#ffff33"),
    strip.text=element_text(color="#a65628")
)

Theme β€’ Rect

element_rect(fill=NULL,color=NULL,size=NULL,linetype=NULL)
p <- p + theme(
    plot.background=element_rect(fill="#b3e2cd"),
    panel.background=element_rect(fill="#fdcdac"),
    panel.border=element_rect(fill=NA,color="#cbd5e8",size=3),
    legend.background=element_rect(fill="#f4cae4"),
    legend.box.background=element_rect(fill="#e6f5c9"),
    strip.background=element_rect(fill="#fff2ae")
)

Theme β€’ Reuse

newtheme <- theme_bw() + theme(
  axis.ticks=element_blank(),
  panel.background=element_rect(fill="white"),
  panel.grid.minor=element_blank(),
  panel.grid.major.x=element_blank(),
  panel.grid.major.y=element_line(size=0.3,color="grey90"),
  panel.border=element_blank(),
  legend.position="top",
  legend.justification="right"
)
p

p + newtheme

Legend β€’ Theme

Legend β€’ Geom

Combining Plots

p <- ggplot(penguins,aes(x=species,y=bill_length_mm,color=island))+geom_point()
q <- ggplot(penguins,aes(x=species,y=body_mass_g,fill=island))+geom_bar(stat="identity")
patchwork::wrap_plots(p,q)

Refer to patchwork documentation. Some notable alternatives are ggpubr and cowplot.

Saving plots

p <- ggplot(iris,aes(Petal.Length,Sepal.Length,color=Species))+
  geom_point()
  • ggplot2 plots can be saved just like base plots
png("plot.png",height=5,width=7,units="cm",res=200)
print(p)
dev.off()
  • ggplot2 package offers a convenient function
ggsave("plot.png",p,height=5,width=7,units="cm",dpi=200,type="cairo")
  • Note that default units in png is pixels while in ggsave it’s inches

Extensions

  • ggrepel: Text labels including overlap control
  • ggforce: Circles, splines, hulls, voronoi etc
  • ggpmisc: Miscellaneaous features
  • ggthemes: Set of extra themes
  • ggthemr: More themes
  • ggsci: Color palettes for scales
  • ggmap: Dedicated to mapping
  • ggraph: Network graphs
  • ggiraph: Converting ggplot2 to interactive graphics

A collection of ggplot extension packages: https://exts.ggplot2.tidyverse.org/.
Curated list of ggplot2 links: https://github.com/erikgahner/awesome-ggplot2.

Learning & Help

Thank you. Questions?