从ggplot facet标签中删除NA

时间:2017-08-03 01:53:47

标签: r ggplot2 label facet

我有一个data.frame我希望ggplotfacets

library(dplyr)

set.seed(1)
samples <- c("mt1.2m_1","mt1.2m_2","mt1.4m_1","mt1.4m_2","mt1.4m_3","mt1.4m_4","mt1.4m_5","mt1.6m_1","mt1.6m_2","mt1.6m_3","mt1.6m_4","mt1.6m_5",
             "mt2.2m_1","mt2.2m_2","mt2.4m_1","mt2.4m_2","mt2.6m_1","mt2.6m_2",
             "wt.2m_1","wt.2m_2","wt.2m_3","wt.2m_4","wt.2m_1","wt.4m_2","wt.4m_3","wt.4m_4","wt.6m_1","wt.6m_2","wt.6m_3","wt.6m_4")
val <- rnorm(30)

df <- data.frame(sample=samples,val=rnorm(length(samples)),gt2m=NA,gt4m=NA,gt6m=NA,stringsAsFactors=F)
df$replicate <- as.integer(gsub("^.*_","",df$sample,perl=T))
df$gt2m[which(grepl("2m_",df$sample))] <- gsub("_\\d+$","",df$sample[which(grepl("2m_",df$sample))],perl=T)
df$gt4m[which(grepl("4m_",df$sample))] <- gsub("_\\d+$","",df$sample[which(grepl("4m_",df$sample))],perl=T)
df$gt6m[which(grepl("6m_",df$sample))] <- gsub("_\\d+$","",df$sample[which(grepl("6m_",df$sample))],perl=T)

df$gt2m <- factor(df$gt2m,levels=c("mt1.2m","mt2.2m","wt.2m"))
df$gt4m <- factor(df$gt4m,levels=c("mt1.4m","mt2.4m","wt.4m"))
df$gt6m <- factor(df$gt6m,levels=c("mt1.6m","mt2.6m","wt.6m"))

#add error bar values
val.error <- 0.05*mean(abs(df$val))
df$min.val <- df$val-3*val.error
df$low.val <- df$val-val.error
df$max.val <- df$val+3*val.error
df$high.val <- df$val+val.error

#add a factor to color by
df$col.fill <- factor(apply(dplyr::select(df,gt2m,gt4m,gt6m),1,function(x) x[which(!is.na(x))]))

我希望通过稀疏的gt2mgt4mgt6m列来完成绘图。我为此定义forumla

facet.formula <- as.formula("~gt2m+gt4m+gt6m")

以下是我试图绘制此内容的方法:

library(ggplot2)

ggplot(df,aes(x=replicate,ymin=min.val,lower=low.val,middle=val,upper=high.val,ymax=max.val,col=col.fill,fill=col.fill))+
  geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
  facet_wrap(scales="free_x",facet.formula,ncol=length(levels(df$col.fill)),labeller = function (labels) {
    labels <- lapply(labels, as.character)
    list(do.call(paste, c(labels, list(sep = ":"))))})

这给了我: enter image description here

除了NA标签中的facet以外,这几乎是完美的。

我试过了:

ggplot(df,aes(x=replicate,ymin=min.val,lower=low.val,middle=val,upper=high.val,ymax=max.val,col=col.fill,fill=col.fill))+
  geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
  facet_wrap(scales="free_x",facet.formula,ncol=length(levels(df$col.fill)),labeller = function (labels) {
    labels <- lapply(labels, as.character)
    list(do.call(paste, c(labels[which(labels != "NA")], list(sep = ":"))))})

但是这会得到相同的结果。

知道如何从NA标签中删除这些facet吗?

2 个答案:

答案 0 :(得分:1)

嗯,我得到了肮脏的修复,但它在某种程度上工作:

library(ggplot2)

ggplot(df,aes(x=replicate,ymin=min.val,lower=low.val,middle=val,upper=high.val,ymax=max.val,col=col.fill,fill=col.fill))+
  geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
  facet_wrap(scales="free_x",facet.formula,ncol=length(levels(df$col.fill)),labeller = function (labels) {
    labels <- lapply(labels, as.character)
    a <-  do.call(paste, c(labels, list(sep = ":")))
    list(gsub(":NA","",(gsub("NA:","",a))))
    })

我已使用NA:函数删除了:NAgsub()个字符,无论如何它看起来都不是很优雅但是有效......

使用正则表达式获得更好的解决方案:

ggplot(df,aes(x=replicate,ymin=min.val,lower=low.val,middle=val,upper=high.val,ymax=max.val,col=col.fill,fill=col.fill))+
  geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
  facet_wrap(scales="free_x",facet.formula,ncol=length(levels(df$col.fill)),labeller = function (labels) {
    labels <- lapply(labels, as.character)
    a <-  do.call(paste, c(labels, list(sep = ":")))
    list(gsub("\\:NA|NA:","",a))
    })

答案 1 :(得分:0)

我看不到快速修复。这里有关于重命名构面标签的讨论: ggplot: How to change facet labels?

希望这是正确的方向