rggplot2jitter

Adding a nudge_y to the data labels of a "Jittered" R ggplot2 graph - Categorical values


Yesterday I posted a question, looking for a special jittered graph a nudge_y on the labels, so as to move them on an upper part of the graph.

Former question Stack Overflow

We were able to fix up my problem, adding jitter to the original dataframe.

Now I am facing something similar, but the y-variable is not qualitative continual, but categorical. Just to make it simple I take the data from iris database.

library(ggrepel)
library(ggplot2)
library(tidyverse)
attach(iris)

pos <- position_jitter(seed = 1, height = 0.25)

ggplot(iris, aes(x=Sepal.Length, y=Species, label = Sepal.Width), max.overlaps = Inf) +
  geom_jitter(position = pos) +
  geom_text_repel(aes(label = ifelse(Sepal.Width > 3, Sepal.Width, "")), position = pos)

Obtaining this graph

enter image description here

I would like to define now a nudge_y on geom_text_repel in order to shift along y-direction only the labels (or also without using nudge_y but with a different trick that I do not know). With this kind of variable I cannot add jittering on the dataframe because Species is categorical variable.

Using aes(y = Sepal.Width + 0.5, label = ifelse(Sepal.Width > 3, Sepal.Width, "")) jitters the labels but they miss the connection with the points:

enter image description here

Is there any way to do this?


Solution

  • Works pretty much the same with categorical variables, you just have to make the factor into a numeric variable and then add the labels later:

    library(ggrepel)
    library(ggplot2)
    library(tidyverse)
    
    
    iris %>% 
      mutate(jit_species = as.numeric(Species) + runif(n(), -0.25, 0.25)) %>% 
      ggplot(aes(x = Sepal.Length, y = jit_species, label = Sepal.Width)) +
      geom_point() +
      geom_text_repel(aes(label = ifelse(Sepal.Width > 3, Sepal.Width, "")),
                      max.overlaps = Inf,
                      nudge_y = .25) +
      scale_y_continuous(breaks = 1:3, 
                         label = levels(iris$Species))