mirror.ticks-deprecated: Put axis ticks on all four sides of a ggplot

Description Usage Arguments Details Value Note Author(s) Examples

Description

Given a ggplot with axes on the bottom and left, add matching axes on the top and right.

DEPRECATED: Use mirror_ticks for most plots, or mirror_gtable if you need it to return a grob.

Usage

1
	mirror.ticks(ggobj, allPanels=FALSE)

Arguments

ggobj

A ggplot object.

allPanels

Logical: Sets behavior in multipanel plots. If TRUE, put ticks on all four sides of every panel. If FALSE, only add them at the right end of each row and the top of each column.

Details

mirror.ticks converts its argument from a ggplot to a gtable, so do all your ggplot adjustments upstream and call this as the last step before plotting.

Use care when mirroring ticks in multipanel plots, especially those constructed using facet_wrap. mirror.ticks does not adjust panel spacing, so any outward-facing ticks it adds are VERY likely to overlap with adjacent panels, hide behind facet labels, or otherwise prove that the last thing most multipanel plots need is more tick marks.

For plots made using facet_wrap(..., scales="free"), the notion of “only mirror ticks to the other end of the row/column” doesn't make much sense and you will almost certainly want to set allPanels=TRUE if you use mirror.ticks at all.

For fixed-scale multipanel plots where the bottom row of panels is not completely full (e.g. 10 panels in a 3 row * 4 column layout), the ggplot default is to display no x-axis in the columns above the empty spaces. Note that mirror.ticks always overrides this (even when allPanels=FALSE) and copies the x-axis from the last panel of the bottom row to the bottom panel of each unfilled column.

Value

A gtable object.

Note

The reason to put ticks on all edges is that with only two axes, people reading the graph may have trouble estimating the position of points that sit far from the axis. Many of the standard ggplot themes use an in-panel background grid to address the same problem, and this author thinks it looks kinda silly to use both at once. I suggest only using mirror.ticks if your ggplot theme has no panel grid.

Author(s)

Chris Black <[email protected]>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## Not run: 
# a simple example plot
a=ggplot(mtcars, aes(wt, mpg))+geom_point()

# ticks are redundant when panel grid is present...
plot(mirror.ticks(a+theme_bw()))

# ...but can help guide the viewer's eye when there is no grid.
theme_nogrid = theme_bw()+theme(
	panel.grid.major = element_blank(),
	panel.grid.minor = element_blank(),
	axis.ticks.length = unit(-0.25 , "lines"),
	axis.ticks.margin = unit(0.5 , "lines"))
plot(mirror.ticks(a+theme_nogrid))

## Multipanel demo
# facet_grid always applies same scale to a whole row or column
# so ticks at each end are usually enough...
a_grid = a+facet_grid(am~carb)+theme_nogrid
plot(mirror.ticks(a_grid))
# ...but it may sometimes be helpful to add them everywhere
plot(mirror.ticks(a_grid, allPanels=TRUE))

# facet_wrap with free scales and default allPanels:
# Ugly, but technically does what you asked it to do...
a_wrap = a+facet_wrap(~carb, scales="free_y")+theme_nogrid
plot(mirror.ticks(a_wrap, allPanels=FALSE))
# ... but you probably wanted ticks everywhere.
plot(mirror.ticks(a_wrap, allPanels=TRUE))

## End(Not run)

infotroph/ggplotTicks documentation built on May 17, 2017, 4:16 a.m.