# A standard plot
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
# Guide as secondary axis
p + guides(x.sec = "axis_scalebar")
# Customising size and label
p + guides(x.sec = guide_axis_scalebar(size = 0.5, label = "0.5 litre"))
# Placing the scale bar on top of the plotting panel
p + guides(x.sec = guide_axis_scalebar(just = 0.95)) +
theme(axis.ticks.length.x.top = unit(-2, "lines"))
# Adding arrows through the axis.ticks theme element
p + guides(y.sec = guide_axis_scalebar(size = 10, label = "10\nmpg")) +
theme(axis.ticks.y.right = element_line(arrow = arrow(ends = "both")))
Run the code above in your browser using DataLab