I am facing a smal issue concerning positioning labels on an R graph. I made an example with the data available in ggplot2
package to make it executable.
library(ggrepel)
library(ggplot2)
library(tidyverse)
pos <- position_jitter(seed = 1, height = 0.25)
ggplot(mtcars, aes(vs, wt, group = am, label = wt)) +
geom_jitter(position = pos) +
geom_text_repel(aes(label = ifelse(wt > 3, wt, "")), position = pos)
I used the command position_jitter
to define jitter and geom_text_repel
to set data labels. Using position_jitter
I cannot define a position nudge_y
of data labels but I woukld like to add to the defined position (position_jitter(seed = 1, height = 0.25)
) a fixed value, let's say +0.5, so as to shift up systematically every label of a defined value.
Is it possible to make in R something like this?
Thank you in advance for every your reply!
RE: comments
You can manually add jitter (random uniform) noise to your datapoints beforehand. Nudging the text then becomes trivial.
library(ggrepel)
library(ggplot2)
library(tidyverse)
mtcars %>%
mutate(jit_vs = vs + runif(length(vs), -0.4, 0.4),
jit_wt = wt + runif(length(wt), -0.25, 0.25)) %>%
ggplot(aes(jit_vs, jit_wt, group = am, label = wt)) +
geom_point() +
geom_text_repel(aes(label = ifelse(wt > 3, wt, "")), nudge_y = 0.5)
Created on 2021-09-03 by the reprex package (v2.0.1)