R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。

huangapple go评论65阅读模式
英文:

R ggplot2: odd behaviour of geom_quasirandom when colouring by a factor

问题

I am a regular and happy user of geom_quasirandom from the ggbeeswarm package to make scatterplots where X is a factor. However, I think some recent updates or something I am doing is causing some odd behaviour when colouring dots. The same script was returning a good-looking figure not long ago.

A reproducible example:

library(ggbeeswarm)

# this is correctly placing the dots:
ggplot(mtcars, aes(x=factor(gear), y=drat)) +
  geom_quasirandom()

# but when colouring dots by a factor, some of the dots get lined up vertically,
# instead of being placed in a violin-like shape
ggplot(mtcars, aes(x=factor(gear), y=drat, color=factor(cyl))) +
  geom_quasirandom()
# (notice particularly the third group of dots, see screenshot below)

# mysteriously, the width argument does not apply to these three dots lined up vertically
# e.g. command below does not move these dots
ggplot(mtcars, aes(x=factor(gear), y=drat, color=factor(cyl))) +
  geom_quasirandom(width=1)

Can you help?

R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。

英文:

I am a regular and happy user of geom_quasirandom from the ggbeeswarm package to make scatterplots where X is a factor. However, I think some recent updates or something I am doing is causing some odd behaviour when colouring dots. The same script was returning a good-looking figure not long ago.

A reproducible example:

library(ggbeeswarm)

# this is correctly placing the dots:
ggplot(mtcars, aes(x=factor(gear), y=drat)) +
  geom_quasirandom()

# but when colouring dots by a factor, some of the dots get ligned up vertically,
# instead of being placed in a violin-like shape
ggplot(mtcars, aes(x=factor(gear), y=drat, color=factor(cyl))) +
  geom_quasirandom()
# (notice particularly the third group of dots, see screenshot below)

# mysteriously, the width argument does not apply to these three dots ligned up vertically
# e.g. command below does not move these dots
ggplot(mtcars, aes(x=factor(gear), y=drat, color=factor(cyl))) +
  geom_quasirandom(width=1)

Can you help?

R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。

答案1

得分: 4

要使您的点着色而不影响定位,您必须明确设置group aes,以便所有点都被视为一个组,使用group=1或仅根据x轴变量分组(本来希望这样做会有所不同,但看起来似乎没有关系):

library(ggbeeswarm)
#> Loading required package: ggplot2

ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = 1)) +
  geom_quasirandom()

R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。<!-- -->


ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = factor(gear))) +
  geom_quasirandom()

R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。<!-- -->

英文:

To color your dots without affecting the positioning you have to explicitly set the group aes so that all points are treated as one group using group=1 or by grouping by the x axis variable only (Would have expected that it does make a difference but looks like that doesn't matter):

library(ggbeeswarm)
#&gt; Loading required package: ggplot2

ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = 1)) +
  geom_quasirandom()

R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。<!-- -->


ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = factor(gear))) +
  geom_quasirandom()

R ggplot2中的geom_quasirandom在按因子着色时表现奇怪。<!-- -->

答案2

得分: 2

这个问题是ggbeeswarm的作者提出的,这个行为在v0.7.1中是一个错误,应该在v0.7.2中修复,我已经提交到CRAN。如果这仍然是一个问题,请告诉我!

英文:

Author of ggbeeswarm here- this behavior is a bug in v0.7.1 and should be fixed in v0.7.2, which I’ve just pushed to CRAN. Please let me know if this remains an issue!

huangapple
  • 本文由 发表于 2023年4月17日 18:35:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定