Home > Net >  Align ggplot2 points without having them overlap?
Align ggplot2 points without having them overlap?

Time:04-26

Is there any possible way to make the points on a boxplot show and not have them overlap each other if they arent unique?

Currently:

enter image description here

I want it to look like this (with the colours and other features):

enter image description here

I tried beeswarm and I'm getting the error: Warning in f(...) : The default behavior of beeswarm has changed in version 0.6.0. In versions <0.6.0, this plot would have been dodged on the y-axis. In versions >=0.6.0, grouponX=FALSE must be explicitly set to group on y-axis. Please set grouponX=TRUE/FALSE to avoid this warning and ensure proper axis choice.

even though I have geom_beeswarm(grouponY=TRUE)

CodePudding user response:

You could do something like this ...

library(tidyverse)

tibble(x = "label", y = rnorm(100, 10, 10)) |> 
  ggplot(aes(x, y))  
  geom_jitter(width = 0.1)  
  geom_boxplot(outlier.shape = NA)

Created on 2022-04-24 by the reprex package (v2.0.1)

CodePudding user response:

Slight modifications to Carl's answer could be to:

  • Move the geom_jitter layer below the geom_boxplot layer, so that the points show through the box; or
  • Make the box more transparent to allow the points to be visible
tibble(x = "label", y = rnorm(100, 10, 10)) %>%  
  ggplot(aes(x, y))  
  geom_boxplot(outlier.shape = NA, alpha )  
  geom_jitter(width = 0.1)

Alternatively, have you considered using a violin plot? It can more effectively show the density of the distribution, as the width of the plot is proportional to the proportion of data points around that level (for the y axis).

  • Related