|
|
1
Anonymous coward
6 年前
您可以使用
gemo rect
并设置分区。我最初把
a和
b作为您的
rects
factors,但是为了匹配您的
type中的颜色,fill just set them to
a
and
c
。
value=runif(n=1000)
类型=rep(c(“a”、“b”、“c”、“d”),250)
类型2=代表(C(“A”,“B”),500)
数字=样品(1:4,1000,替换=真,Prob=C(0.25,0.25,0.25,0.25,0.25))
特征=C(rep(“小”,500),rep(“大”,500))
NFAC<-4在此定义因子(类型)的数量
rects<-data.frame(xmin=head(seq<-seq(0.5,nfac+.5,1),-1),
xmax=tail(seq,-1),rect_type=c(“a”,“c”))在此处设置您的分区
所有结果<-data.frame(值、类型、类型2、数字、功能、矩形)
ggplot(所有结果,aes(y=值,x=类型))+geom_boxplot(aes(fill=类型,col=类型2))。+
几何矩形(aes(xmin=xmin,xmax=xmax,ymin=-inf,ymax=inf,fill=rect_type),alpha=0.009)+
ggtitle(“比较”)+facet_grid(功能~编号)+
主题(legend.position=“bottom”,axis.text.x=element_text(angle=90,hjust=1))。+
连续缩放(中断=序列(0,1,x=0.05),限制=C(0,1))
< /代码>

以匹配您的type 填充只需将它们设置为一 和c .
value = runif(n = 1000)
type = rep(c("a","b","c","d"),250)
type2 = rep(c("a","b"),500)
number = sample(1:4, 1000, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
feature = c(rep("small",500),rep("big",500))
nFac <- 4 # define number of factors (types) here
rects <- data.frame(xmin = head(seq <- seq(0.5, nFac + .5, 1), -1),
xmax = tail(seq, -1), rect_type = c("a", "c")) #set your divisions here
allResults <- data.frame(value,type,type2,number,feature, rects)
ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(aes(fill = type, col=type2)) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf, fill = rect_type), alpha = 0.009) +
ggtitle("comparison") + facet_grid(feature ~ number) +
theme(legend.position = "bottom",axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous(breaks = seq(0, 1, by = 0.05),limits = c(0,1))

|