在 boxplot/dotplot 中使用 geom_count 表示具有多个组的多个值实例
Use geom_count in a boxplot/dotplot to indicate multiple instances of value with multiple groups
我想在箱线图上绘制我的数据分布图。到目前为止,我已经设法将 geom_boxplot()
与 geom_dotplot()
重叠。但是,我有很多数据点,有很多重叠。我想在我的情节中给出一些关于大多数值在哪里的指示。我认为 geom_count 可以提供帮助。但是,我还没有找到将它用于多个组的方法!
这是我第一次尝试 boxplot/dotplot:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8),
dotsize=0.7) +
scale_fill_grey() + theme_classic()
现在我想添加 geom_count,它每次只给出一个结果,而不是每组:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8),
dotsize=0.7) +
scale_fill_grey() + theme_classic() +
geom_count(aes(x=time, y=BDI, group=Groups))
如何使不同颜色的组点大小不同?或任何其他显示重叠的方法?
这可能不是最优雅的解决方案,但您可以使用空心圆形,这样您就可以重叠点,并在需要时减小点的大小。使用参数 shape 和 size 然后减少位置闪避。
您还可以增加箱线图之间的 space:
Spacing between boxplots in ggplot2
编辑:这是一个使用 geom_point()
和 position_jitterdodge()
而不是 geom_dotplot()
的可重现示例。
#just creating a reproducible example:
rdu<-function(n,k) sample(1:k,n,replace=T)
time<-rdu(300,30)
data_BDI<-data.frame(BDI=time,time=rep(c("BDI","BDI.FU","BDI.FU2"),each=100),Groups=rep(rep(c("ABM","both","control","BMS"),each=25),3))
解决方案 1 使用 geom_point()
:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
geom_point(aes(fill = Groups), size = 2, shape = 1, position = position_jitterdodge())+
# geom_dotplot(binaxis='y',
# stackdir='center',
# position=position_dodge(0.8),
# dotsize=0.7,
# shape=1) +
scale_fill_grey()+
theme_classic()
解决方案 2 使用 geom_count()
:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
scale_fill_grey()+
theme_classic()+
geom_count(aes(fill = Groups), position = position_jitterdodge())
方案三:
如果你想让点对齐,只需使用这些参数:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
scale_fill_grey()+
theme_classic()+
geom_count(aes(fill = Groups), position = position_jitterdodge(0,0,0.81))
您也可以尝试 ggbeeswarm
解决方案。相反,您必须使用 tidyverse
自己计算 n
library(tidyverse)
library(ggbeeswarm)
data_BDI %>%
group_by(time, Groups,BDI) %>% # grouping to calculate the counts of duplicates
add_count() %>% # the calculation
ggplot(aes(x=Groups, y=BDI, fill=Groups)) +
geom_boxplot() +
# remove duplicates to keep the plot clean
geom_beeswarm(data=. %>% distinct(), aes(size=n)) +
facet_grid(~time) +
guides(fill = "none")
数据
set.seed(1233)
rdu<-function(n,k) sample(1:k,n,replace=T)
time<-rdu(300,30)
data_BDI<-data.frame(BDI=time,time=rep(c("BDI","BDI.FU","BDI.FU2"),each=100),Groups=rep(rep(c("ABM","both","control","BMS"),each=25),3))
我想在箱线图上绘制我的数据分布图。到目前为止,我已经设法将 geom_boxplot()
与 geom_dotplot()
重叠。但是,我有很多数据点,有很多重叠。我想在我的情节中给出一些关于大多数值在哪里的指示。我认为 geom_count 可以提供帮助。但是,我还没有找到将它用于多个组的方法!
这是我第一次尝试 boxplot/dotplot:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8),
dotsize=0.7) +
scale_fill_grey() + theme_classic()
现在我想添加 geom_count,它每次只给出一个结果,而不是每组:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8),
dotsize=0.7) +
scale_fill_grey() + theme_classic() +
geom_count(aes(x=time, y=BDI, group=Groups))
如何使不同颜色的组点大小不同?或任何其他显示重叠的方法?
这可能不是最优雅的解决方案,但您可以使用空心圆形,这样您就可以重叠点,并在需要时减小点的大小。使用参数 shape 和 size 然后减少位置闪避。
您还可以增加箱线图之间的 space: Spacing between boxplots in ggplot2
编辑:这是一个使用 geom_point()
和 position_jitterdodge()
而不是 geom_dotplot()
的可重现示例。
#just creating a reproducible example:
rdu<-function(n,k) sample(1:k,n,replace=T)
time<-rdu(300,30)
data_BDI<-data.frame(BDI=time,time=rep(c("BDI","BDI.FU","BDI.FU2"),each=100),Groups=rep(rep(c("ABM","both","control","BMS"),each=25),3))
解决方案 1 使用 geom_point()
:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
geom_point(aes(fill = Groups), size = 2, shape = 1, position = position_jitterdodge())+
# geom_dotplot(binaxis='y',
# stackdir='center',
# position=position_dodge(0.8),
# dotsize=0.7,
# shape=1) +
scale_fill_grey()+
theme_classic()
解决方案 2 使用 geom_count()
:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
scale_fill_grey()+
theme_classic()+
geom_count(aes(fill = Groups), position = position_jitterdodge())
方案三: 如果你想让点对齐,只需使用这些参数:
ggplot(data_BDI, aes(x=time, y=BDI, fill=Groups)) +
geom_boxplot(position=position_dodge(0.8))+
scale_fill_grey()+
theme_classic()+
geom_count(aes(fill = Groups), position = position_jitterdodge(0,0,0.81))
您也可以尝试 ggbeeswarm
解决方案。相反,您必须使用 tidyverse
n
library(tidyverse)
library(ggbeeswarm)
data_BDI %>%
group_by(time, Groups,BDI) %>% # grouping to calculate the counts of duplicates
add_count() %>% # the calculation
ggplot(aes(x=Groups, y=BDI, fill=Groups)) +
geom_boxplot() +
# remove duplicates to keep the plot clean
geom_beeswarm(data=. %>% distinct(), aes(size=n)) +
facet_grid(~time) +
guides(fill = "none")
数据
set.seed(1233)
rdu<-function(n,k) sample(1:k,n,replace=T)
time<-rdu(300,30)
data_BDI<-data.frame(BDI=time,time=rep(c("BDI","BDI.FU","BDI.FU2"),each=100),Groups=rep(rep(c("ABM","both","control","BMS"),each=25),3))