英文:
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?
英文:
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?
答案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()
<!-- -->
ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = factor(gear))) +
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)
#> Loading required package: ggplot2
ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = 1)) +
geom_quasirandom()
<!-- -->
ggplot(mtcars, aes(x = factor(gear), y = drat, color = factor(cyl), group = factor(gear))) +
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论