abruh
abruh

Reputation: 93

How do I make a ggplot graph with different x values having different jitter?

If I want to jitter points, but my x axis has uneven intervals like this:

library(ggplot2)
ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_point()

enter image description here

how can I vary the widths of the jitter at different x axis values so I don't end up with overlap like this?

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter(width = 0.3,size = 4)

enter image description here

I want a way for instance to make the jitter narrow at 0.5, wider at 1, and widest at 2.

Upvotes: 0

Views: 241

Answers (1)

Likan Zhan
Likan Zhan

Reputation: 1076

A dirty way might be to jitter the dose before the plotting:

JitterSize <- rep(c(1.5, 3, 6), table(ToothGrowth$dose))
ToothGrowth $ Jitter <- jitter(ToothGrowth $ dose, JitterSize)
ggplot(data = ToothGrowth, 
       aes(x = Jitter, y=len, color = as.character(dose))) + 
geom_point(size = 4)

Updated based on abruh's comments.

Upvotes: 1

Related Questions