在geom_boxplot上覆盖geom_points()(fill = group)?

时间:2022-06-15 13:06:40

Here is my code at the moment:

这是我目前的代码:

require(ggplot2)
value <- rnorm(40, mean = 10, sd = 1)
variable <- c(rep('A', 20), rep('B', 20))
group <- rep(c('Control', 'Disease'), 20)
data <- data.frame(value, variable, group)

ggplot(data, aes(x=variable, y=value)) +
  geom_boxplot(aes(fill=group)) +
  geom_point(aes())

This divides up boxplots into groups by variable the way I'd like. However, the points for all groups are overlaid, and I'd like for it to be divided up into groups. How would I go about doing this?

这会按照我想要的方式将箱图分成不同的组。但是,所有组的分数都是重叠的,我希望将它分成几组。我该怎么做呢?

4 个解决方案

#1


10  

Use position_dodge() for the points and also add group=group inside aes() of geom_point().

对position使用position_dodge(),并在geom_point()的aes()中添加group = group。

ggplot(data, aes(x=variable, y=value)) +
  geom_boxplot(aes(fill=group)) +
  geom_point(position=position_dodge(width=0.75),aes(group=group))

在geom_boxplot上覆盖geom_points()(fill = group)?

#2


3  

I don't know when this was introduced, but there is a new(ish) featured called position_jitterdodge, which simplifies this, whether you want jittering or not. Usage:

我不知道这是什么时候推出的,但有一个名为position_jitterdodge的新功能,无论你是否想要抖动,都可以简化这一过程。用法:

ggplot(data, aes(x=variable, y=value, fill=group)) +
  geom_boxplot() +
  geom_point(position=position_jitterdodge())
  # or, if you dont need jittering
  # geom_point(position=position_jitterdodge(jitter.width = 0, jitter.height = 0)) 

在geom_boxplot上覆盖geom_points()(fill = group)?

http://ggplot2.tidyverse.org/reference/position_jitterdodge.html

#3


0  

You can try the ggbeeswarm as well. Here I compare the output of geom_beeswarm and geom_quasirandom:

你也可以试试ggbeeswarm。在这里,我比较geom_beeswarm和geom_quasirandom的输出:

library(ggbeeswarm)
library(ggplot2)

ggplot(data, aes(x=variable, y=value, fill=group)) +
  geom_boxplot() +
  geom_beeswarm(dodge.width=0.75) +
  geom_quasirandom(dodge.width=.75, col=2) 

在geom_boxplot上覆盖geom_points()(fill = group)?

#4


-1  

Here is an attempt to apply Didzis's suggestion to a dataset where not all groups have an outlier and thus the points don't line up with the correct box. 在geom_boxplot上覆盖geom_points()(fill = group)?

这是尝试将Didzis的建议应用于数据集,其中并非所有组都有异常值,因此这些点不与正确的框对齐。

Data file: https://cmu.box.com/shared/static/2hxp2oms5et1ktr9hukdr539b6svq1cg.rds

数据文件:https://cmu.box.com/shared/static/2hxp2oms5et1ktr9hukdr539b6svq1cg.rds

require(data.table)
require(ggplot2)

d <- readRDS("2hxp2oms5et1ktr9hukdr539b6svq1cg.rds")

ggplot(d) + 
  geom_boxplot(aes(x=factor(game),y=N,fill=.group),outlier.shape=NA) + 
  geom_point(data=dd.sum1[,.SD[which(N %in% boxplot.stats(N)$out)],by=game][,.group:=factor(.group,levels=.groups)][,],aes(x=factor(game),y=N,color=.group,group=.group),
             position=position_dodge(width=0.75),
             size=.5) +
  facet_grid (type~., scales="free_x", space="free_x") +
  xlab("Game number") +
  ylab("Count") +
  scale_color_brewer("Group",palette="Set1",drop=FALSE) +
  scale_fill_brewer("Group",palette="Set1",drop=FALSE) +
  theme(axis.text.x=element_text(size=7),legend.position="top")

#1


10  

Use position_dodge() for the points and also add group=group inside aes() of geom_point().

对position使用position_dodge(),并在geom_point()的aes()中添加group = group。

ggplot(data, aes(x=variable, y=value)) +
  geom_boxplot(aes(fill=group)) +
  geom_point(position=position_dodge(width=0.75),aes(group=group))

在geom_boxplot上覆盖geom_points()(fill = group)?

#2


3  

I don't know when this was introduced, but there is a new(ish) featured called position_jitterdodge, which simplifies this, whether you want jittering or not. Usage:

我不知道这是什么时候推出的,但有一个名为position_jitterdodge的新功能,无论你是否想要抖动,都可以简化这一过程。用法:

ggplot(data, aes(x=variable, y=value, fill=group)) +
  geom_boxplot() +
  geom_point(position=position_jitterdodge())
  # or, if you dont need jittering
  # geom_point(position=position_jitterdodge(jitter.width = 0, jitter.height = 0)) 

在geom_boxplot上覆盖geom_points()(fill = group)?

http://ggplot2.tidyverse.org/reference/position_jitterdodge.html

#3


0  

You can try the ggbeeswarm as well. Here I compare the output of geom_beeswarm and geom_quasirandom:

你也可以试试ggbeeswarm。在这里,我比较geom_beeswarm和geom_quasirandom的输出:

library(ggbeeswarm)
library(ggplot2)

ggplot(data, aes(x=variable, y=value, fill=group)) +
  geom_boxplot() +
  geom_beeswarm(dodge.width=0.75) +
  geom_quasirandom(dodge.width=.75, col=2) 

在geom_boxplot上覆盖geom_points()(fill = group)?

#4


-1  

Here is an attempt to apply Didzis's suggestion to a dataset where not all groups have an outlier and thus the points don't line up with the correct box. 在geom_boxplot上覆盖geom_points()(fill = group)?

这是尝试将Didzis的建议应用于数据集,其中并非所有组都有异常值,因此这些点不与正确的框对齐。

Data file: https://cmu.box.com/shared/static/2hxp2oms5et1ktr9hukdr539b6svq1cg.rds

数据文件:https://cmu.box.com/shared/static/2hxp2oms5et1ktr9hukdr539b6svq1cg.rds

require(data.table)
require(ggplot2)

d <- readRDS("2hxp2oms5et1ktr9hukdr539b6svq1cg.rds")

ggplot(d) + 
  geom_boxplot(aes(x=factor(game),y=N,fill=.group),outlier.shape=NA) + 
  geom_point(data=dd.sum1[,.SD[which(N %in% boxplot.stats(N)$out)],by=game][,.group:=factor(.group,levels=.groups)][,],aes(x=factor(game),y=N,color=.group,group=.group),
             position=position_dodge(width=0.75),
             size=.5) +
  facet_grid (type~., scales="free_x", space="free_x") +
  xlab("Game number") +
  ylab("Count") +
  scale_color_brewer("Group",palette="Set1",drop=FALSE) +
  scale_fill_brewer("Group",palette="Set1",drop=FALSE) +
  theme(axis.text.x=element_text(size=7),legend.position="top")