代码之家  ›  专栏  ›  技术社区  ›  TheAvenger

r ggplot背景色方框图

  •  1
  • TheAvenger  · 技术社区  · 6 年前

    我有一个这样的数据框架:

    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))
    所有结果<-data.frame(值、类型、类型2、数字、功能)
    < /代码> 
    
    

    我想将箱线图的背景色设置为type2value。如果我用fill和col,就不太清楚了。如果可能的话,我认为更直观的背景色。

    library(“ggplot2”)。
    ggplot(所有结果,aes(y=值,x=类型))+geom_boxplot(alpha=0.3,aes(fill=类型,col=类型2))。+
    ggtitle(“比较”)+facet_grid(功能~编号)+
    主题(legend.position=“bottom”,axis.text.x=element_text(angle=90,hjust=1))。+
    连续缩放(中断=序列(0,1,x=0.05),限制=C(0,1))
    

    这是我目前的结果:

    我已经看到可以使用geom rect()为背景着色,但我不理解如何应用。

    我想给箱线图的背景上色2型价值。如果我用fill和col,就不太清楚了。如果可能的话,我认为更直观的背景色。

    library("ggplot2")
    ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(alpha=.3, aes(fill = type,col=type2)) +
      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))
    

    这是我目前的结果:

    enter image description here 我已经看到,这是可能的颜色背景使用GeMangSub()但我不明白怎么申请。

    1 回复  |  直到 6 年前
        1
  •  2
  •   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))
    

    enter image description here