user2838808
user2838808

Reputation: 55

How to use ggplot2's geom_dotplot() with symmetrically spaced and separated dots

I am having a problem symmetrically placing the dots in geom_dotplot when stackratio is greater than 1. With stackratio = 1, the dots are symmetrically placed above the ticks marks. With stackratio > 1, the dots are shifted left. Is there a way to keep the dots symmetrically placed while creating space between the dots?

library(ggplot2)

data <- data.frame(x = rep(seq(2018, 2021, 1), 15), 
                   y = sample(seq(3, 5, .125), 60, 
                   replace = T))

ggplot(data, aes(factor(x), y)) +
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1)

ggplot(data, aes(factor(x), y)) +
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 2)

Upvotes: 2

Views: 513

Answers (1)

MrFlick
MrFlick

Reputation: 206546

The problem seems to be in the makeContext.dotstackGrob function. It calculates the offsets for each point with

xpos <- xmm + dotdiamm * (x$stackposition * x$stackratio + (1 - x$stackratio) / 2)

but for the life of me, I cannot see why the (1 - x$stackratio) / 2 part is there. Without it, everyything seems to lineup OK. If I change that line to

xpos <- xmm + dotdiamm * x$stackposition * x$stackratio

and test with the sample data, I get

ggplot(data, aes(factor(x), y)) +
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1)

enter image description here

ggplot(data, aes(factor(x), y)) +
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = .5)

enter image description here

ggplot(data, aes(factor(x), y)) +
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 2)

enter image description here

So maybe this counts a a bug report? Not sure what else to test

Upvotes: 2

Related Questions