+ - 0:00:00
Notes for current slide
Notes for next slide

ggplot Part III

Workshop on Data Visualization in R

Lokesh Mano • 29-Apr-2022

NBIS, SciLifeLab

1/9

Scales • Axes

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

Scales • Axes

  • scales: x, y
  • syntax: scale_<axis>_<type>
  • arguments: name, limits, breaks, labels
p <- ggplot(iris)+
geom_point(aes(x=Sepal.Length,
y=Sepal.Width,
color=Species))
p

3/9

Scales • Axes

  • scales: x, y
  • syntax: scale_<axis>_<type>
  • arguments: name, limits, breaks, labels
p <- ggplot(iris)+
geom_point(aes(x=Sepal.Length,
y=Sepal.Width,
color=Species))
p

p + scale_color_manual(name="New",
values=c("blue","green","red"))+
scale_x_continuous(name="Sepal Length",
breaks=seq(1,8),limits=c(3,5))

3/9

Coordinate Systems

  • coord_cartesian(xlim=c(2,8)) for zooming in
  • coord_map for controlling limits on maps
  • coord_polar
p <- ggplot(iris,aes(x="",y=Petal.Length,fill=Species))+
geom_bar(stat="identity")
p

4/9

The coordinate system defines the surface used to represent numbers. Most plots use the cartesian coordinate sytem. Pie charts for example, is a polar coordinate projection of a cartesian barplot. Maps for example can have numerous coordinate systems called map projections. For example; UTM coordinates.

Coordinate Systems

  • coord_cartesian(xlim=c(2,8)) for zooming in
  • coord_map for controlling limits on maps
  • coord_polar
p <- ggplot(iris,aes(x="",y=Petal.Length,fill=Species))+
geom_bar(stat="identity")
p

p+coord_polar("y",start=0)

4/9

The coordinate system defines the surface used to represent numbers. Most plots use the cartesian coordinate sytem. Pie charts for example, is a polar coordinate projection of a cartesian barplot. Maps for example can have numerous coordinate systems called map projections. For example; UTM coordinates.

Theme

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

Theme

  • Modify non-data plot elements/appearance
  • Axis labels, panel colors, legend appearance etc
  • Save a particular appearance for reuse
  • ?theme
ggplot(iris,aes(Petal.Length))+
geom_histogram()+
facet_wrap(~Species,nrow=2)+
theme_grey()

5/9

Theme

  • Modify non-data plot elements/appearance
  • Axis labels, panel colors, legend appearance etc
  • Save a particular appearance for reuse
  • ?theme
ggplot(iris,aes(Petal.Length))+
geom_histogram()+
facet_wrap(~Species,nrow=2)+
theme_grey()

ggplot(iris,aes(Petal.Length))+
geom_histogram()+
facet_wrap(~Species,nrow=2)+
theme_bw()

5/9

Themes allow to modify all non-data related components of the plot. This is the visual appearance of the plot. Examples include the axes line thickness, the background color or font family.

Theme • Legend

p <- ggplot(iris)+
geom_point(aes(x=Sepal.Length,
y=Sepal.Width,
color=Species))
p + theme(legend.position="top")

p + theme(legend.position="bottom")

6/9

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")
)

7/9

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")
)

8/9

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"
)
9/9

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

9/9

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

9/9

Thank you. Questions?

R version 4.1.3 (2022-03-10)

Platform: x86_64-pc-linux-gnu (64-bit)

OS: Ubuntu 18.04.6 LTS



Built on : 29-Apr-2022 at 07:10:30

2022SciLifeLabNBIS

9/9
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow