Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Option to vertical jitter? #93

Open
mvanaman opened this issue Dec 17, 2023 · 1 comment
Open

Feature Request: Option to vertical jitter? #93

mvanaman opened this issue Dec 17, 2023 · 1 comment

Comments

@mvanaman
Copy link

library(psych)
library(ggplot2)
#> 
#> Attaching package: 'ggplot2'
#> The following objects are masked from 'package:psych':
#> 
#>     %+%, alpha
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(ggbeeswarm)
set.seed(352)
bfi %>% 
  select(starts_with("O")) %>% 
  pivot_longer(cols = everything(), names_to = "facet", values_to = "rating") %>% 
  slice(sample(1:nrow(.), size = 200)) %>% 
  ggplot(aes(x = facet, y = rating)) +
  geom_beeswarm()

Created on 2023-12-17 with reprex v2.0.2

In use cases such as the above plot, it would be cool to be able to vertically jitter the points slightly to get a sense of the Ns of each rating level. Unless there is a way to do this that I missed!

@mvanaman mvanaman changed the title Option to vertical jitter? Feature Request: Option to vertical jitter? Dec 17, 2023
@eclarke
Copy link
Owner

eclarke commented May 12, 2024

Hi @mvanaman, you can do this two ways without using ggbeeswarm . The first is to add jitter manually to the dataframe prior to ggplot- this has the benefit of letting you group the variables with dplyr so the jitter amount is correct. The other way to do it is to simply call jitter() in the aes() argument for your plot, though this ignores any grouping so you'd probably want to manually tune the jitter(amount=...) argument.

Here's an example of both:

library(psych)
library(tidyr)
library(dplyr)

set.seed(352)
dat93 <- bfi %>% 
  select(starts_with("O")) %>% 
  pivot_longer(cols = everything(), names_to = "facet", values_to = "rating") %>% 
  slice(sample(1:nrow(.), size = 200)) %>%
  # "Grouped" jitter
  group_by(facet, factor(rating)) %>%
  mutate(jitter_rating = jitter(rating))

ggplot(dat93, aes(x = facet)) +
  geom_beeswarm(aes(y = jitter_rating, color="grouped jitter"), alpha = 0.5) + 
  # "Within-aesthetic" jitter
  geom_beeswarm(aes(y = jitter(rating), color="within-aesthetic jitter"), alpha = 0.5)

Created on 2024-05-12 with reprex v2.1.0

I personally prefer the first option. I also agree this may be a useful feature to build into ggbeeswarm so I'll keep this open for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants