3

我有要使用ggplot's绘制的实验数据facet_grid

该实验分 6 批收集了来自两个物种、三个条件的测量值:

species <- c("spcies1","species2")
conditions <- c("cond1","cond2","cond3")
batches <- 1:6

df <- expand.grid(species=species,condition=conditions,batch=batches)

测量值是 0 到 1 之间的分数:

set.seed(1)
df$y <- runif(nrow(df),0,1)

在这种情况下,每个物种、条件和批次只有一个重复:

df$replicate <- 1

但这是灵活的。

我想按其物种、条件和批次为每个测量值着色(在这个特定示例中,因为每个观察值都有一个重复,每个观察值都有自己独特的颜色):

df$col.fill <- paste(df$species,df$condition,df$batch,sep=".")

考虑变量:

df$col.fill <- factor(df$col.fill,levels=unique(df$col.fill))
df$species <- factor(df$species,levels=species)
df$condition <- factor(df$condition,levels=conditions)

我想绘制数据,使它们按物种、条件和批次进行分面,并相应地标记各个方面。

这是我正在尝试的: library(ggplot2)

integerBreaks <- function(n = 5, ...)
{
  library(scales)
  breaker <- pretty_breaks(n, ...)
  function(x){
    breaks <- breaker(x)
    breaks[breaks == floor(breaks)]
  }
}


ggplot(df,aes(x=replicate,y=y,color=col.fill))+
geom_point(size=3)+facet_grid(~species+condition+batch,scales="free_x")+
scale_x_continuous(breaks=integerBreaks())+
theme_minimal()+theme(legend.position="none",axis.title=element_text(size=8))

这给了我: 在此处输入图像描述

这样做是重复每批次的物种和条件,因此标签过于拥挤并被削减。有没有办法让物种方面标签每个物种只出现一次,条件方面标签每个条件只出现一次(即,遵循它们的层次结构)?

这意味着我将只有两个物种方面标签和六个条件方面标签(对每个物种重复)。

4

0 回答 0