I've been trying for a while now to put the horizontal axis title on top of the chart after using coord_flip()
in ggplot2, but without any success.
I know I could:
scale_y_continuous (position = "right")
theme (axis.title.x.top = element_text (face = "bold"))
# I guess any format would do here, right?But then:
CODE
example <- iris |>
ggplot (aes (x = reorder (Species, Petal.Width), y = Petal.Width)) +
geom_bar (stat = "identity", fill = "gray60") +
geom_hline (yintercept = 100 / nrow (iris), linetype = "dashed", color = "firebrick", size = 1) +
scale_y_continuous (limits = c(0, 15), breaks = seq (0, 15, 2.5), expand = c(0, 0)) +
coord_flip () +
labs (x = "", y = "Horizontal axis title") +
theme (axis.title = element_text (family = "MS", face = "bold", size = 18, color = "black"),
axis.text = element_text (family = "TNR", face = "plain", size = 16, color = "black"),
axis.text.x = element_text (angle = 45),
panel.background = element_rect (fill = NA),
panel.grid.major = element_line (color = "gray90"),
panel.grid.minor = element_line (color = "gray90"),
plot.margin = margin (t = 20, r = 20, b = 5, l = 5)) +
windows (); example
I'd defer to @stefan's solution for splitting up the axis title and the breaks; that is simplest with a duplicate axis.
I would add, though, that most cases to use coord_flip()
were eliminated with ggplot2 3.3.0 in 2020. (Though occasionally you may need to add the orientation
parameter if the aesthetics don't make it clear).
Then we can do:
iris |>
ggplot(aes(y = reorder (Species, Petal.Width), x = Petal.Width)) +
geom_col(fill = "gray60") +
geom_vline(xintercept = 100 / nrow (iris), linetype = "dashed",
color = "firebrick", size = 1) +
scale_x_continuous(limits = c(0, 15), breaks = seq (0, 15, 2.5),
expand = c(0, 0), name = NULL,
sec.axis = dup_axis(breaks = NULL, name = "Horizontal axis title")) +
labs(y = NULL) # avoids even having a space for the axis title