I have below ggplot
library(dplyr)
library(ggplot2)
dat = data.frame(name=c("apple", "orange", "plum"), value=c(3,8,2), outlier=c(FALSE,TRUE,FALSE))
ggplot(dat, aes(x = value, y = name)) + geom_point() +
scale_x_continuous(limits = c(2, 9), breaks = c(3.10, 2:9))
While this plot is fine, I would like to draw my extra tick-label
at 3.10
in x-axis
little below (say 6 pixel point
below) to the current horizontal level, while keeping all other tick labels at the current level.
I am just wondering if there is any way to achieve that?
Thanks for your time
Besides the option to add the label via an annotation another more recent option would be the legendry
package which allows for stacked axes, i.e. in the code below I use compose_stack
to add the ticks, the labels at regular breaks and the label for the irregular break separately using the primitive_
family of functions:
library(ggplot2)
library(legendry)
dat <- data.frame(
name = c("apple", "orange", "plum"),
value = c(3, 8, 2),
outlier = c(FALSE, TRUE, FALSE)
)
ggplot(dat, aes(x = value, y = name)) +
geom_point() +
scale_x_continuous(
limits = c(2, 9),
breaks = c(3.1, 2:9)
) +
guides(
x = compose_stack(
primitive_ticks(
key = key_auto()
),
primitive_labels(
key = key_manual(
aesthetic = 2:9
)
),
primitive_labels(
key = key_manual(
aesthetic = 3.1
)
)
)
)
But of course a simpler option would be put the label for the irregular break on a second line by prefixing with " \n"
via the labels=
argument of the scale:
ggplot(dat, aes(x = value, y = name)) +
geom_point() +
scale_x_continuous(
limits = c(2, 9),
labels = \(x) ifelse(x == 3.1, paste0(" \n", x), x),
breaks = c(3.1, 2:9)
)