df <- data.frame(
x = c(1,3,2,5,4,2.5),
y = c(2, 1, 2.5, 1.8, 2.8, 1.5),
label = c("abc","cd","d","c","bcd","a")
)
# default does nothing
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text(position = position_nudge_to())
# a single y (or x) value nudges all observations to this data value
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text(position = position_nudge_to(y = 3))
# with a suitable geom, segments or arrows can be added
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position = position_nudge_to(y = 3))
# alternating in y value order because y has fewer values than rows in data
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position = position_nudge_to(y = c(3, 0)))
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position = position_nudge_to(y = c(0, 3)))
# in data row order because y has as many values as rows in data
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position = position_nudge_to(y = rep_len(c(0, 3), 6)))
# spread the values at equal distance within the available space
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 3, x.action = "spread"))
# spread the values at equal distance within the expanded available space
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 3, x.action = "spread", x.expansion = 0.1))
# spread the values at equal distance within the contracted available space
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 3, x.action = "spread", x.expansion = -0.1))
# spread the values at equal distance within the range given by x
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 3, x = c(2,4), x.action = "spread"),
hjust = "center")
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 3, x = c(0,6), x.action = "spread"),
hjust = "center")
Run the code above in your browser using DataLab