# A plot with a crowded y-axis
p <- ggplot(mtcars, aes(mpg, rownames(mtcars))) +
geom_col() +
coord_cartesian(ylim = c(-32, 64)) +
theme(axis.text.y = element_text_repel())
# By default there isn't enough space to draw distinctive lines
p
# The available space can be increased by setting the margin
p + theme(axis.text.y.left = element_text_repel(margin = margin(r = 20)))
# For secondary axis positions at the top and right, the `position` argument
# should be set accordingly
p + scale_y_discrete(position = "right") +
theme(axis.text.y.right = element_text_repel(
margin = margin(l = 20),
position = "right"
))
# Using segment settings and matching tick colour
p + theme(
axis.text.y.left = element_text_repel(
margin = margin(r = 20),
segment.curvature = -0.1,
segment.inflect = TRUE,
segment.colour = "red"
),
axis.ticks.y.left = element_line(colour = "red")
)
Run the code above in your browser using DataLab