# The examples below exemplify the features of position_nudge_to().
# Please see the vignette for examples of use cases.
df <- data.frame(
x = c(1,3,2,5,4,2.5),
y = c(2, 3, 2.5, 1.8, 2.8, 1.5),
grp = c("A", "A", "A", "B", "B", "B"),
grp.inner = c("a", "b", "c", "a", "b", "c"),
label = c("abc","cd","d","c","bcd","a")
)
# default is no nudging
ggplot(df, aes(label, y, label = y)) +
geom_col() +
geom_text(position = position_nudge_to(),
vjust = -0.2)
# a single y (or x) value nudges all observations to this data value
ggplot(df, aes(label, y, label = y)) +
geom_col() +
geom_label(position = position_nudge_to(y = 1))
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text(position = position_nudge_to(y = 3.2))
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_line() +
geom_text(position = position_nudge_to(y = 0.1))
# 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 = 2.25))
# 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.1)))
# in data row order with as many nudge y values as rows in data
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position = position_nudge_to(y = c(1.8, 2.3, 1.3, 2.8, 3, 0.1)))
# 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 = 4, x.action = "spread"))
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 4, x.action = "spread")) +
scale_x_log10()
# 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 = 4, 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 = 4, x = c(1.5, 4), x.action = "spread"))
# currently if scale transformations are used, the x and/or y arguments must
# be transformed. WARNING: This will change in the near future!!
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = 4, x = log10(c(1.5, 4)), x.action = "spread")) +
scale_x_log10()
ggplot(df, aes(x, y, label = label)) +
geom_point() +
geom_text_s(position =
position_nudge_to(y = log10(4), x.action = "spread")) +
scale_y_log10()
Run the code above in your browser using DataLab