2017-03-10 4 views
0

バイオリンプロット(3 obs未満)(図中のBBとCCC)に何も表示しないカテゴリのグラフを削除する方法をリクエストしたいと思います。しかし、サンプル全体のサブプロット全体のすべてのデータを保持してください。データフレームをフィルタリングし、元のコピーを添付する(サンプル全体のサブプロットに対して)より簡単な方法がありますか?geom_violinの関数を使用して小さなサンプルでカテゴリを削除する

# example df 
library(ggplot2) 
b<-abs(round(rnorm(8, sd=30))) 
y<-runif(5) 
pr<-y/sum(y) 
names<-unlist(lapply(mapply(rep, LETTERS[1:5], 1:5), function (x) paste0(x, collapse = ""))) 
x <- sample(names, 8, replace=TRUE, prob=pr) 
x 
df<-data.frame(name=x,numbers=b) 

violinplot_fun <- function(dataset, var, groupcol, adjust1, maxx) { 
    ggplot(dataset)+ 
    geom_violin(aes_string(y = var, x = groupcol), scale = "width", 
       alpha = 0.4, adjust = adjust1) + 
    geom_violin(aes_(y = as.name(var), x = "Whole sample"), scale = "width", 
       alpha = .4, adjust = adjust1) + 
    scale_y_continuous(limits = c(0,ceiling(maxx)) , breaks = scales::pretty_breaks(15)) + 
    coord_flip() 
} 

violinplot_fun(df,"numbers", "name",0.5,100) 

enter image description here

答えて

1

あなたが前data.tableパッケージを使用して機能でそれを呼び出すにデータフレームを編集する場合、あなたはこのようにそれを行うことができます。

dt <- as.data.table(df) 
dt1 <- dt[, n := .N, by = name] 

EDIT私はあなたを変更わずかに機能:

violinplot_fun <- function(dataset, dataset_orig, var, groupcol, adjust1, maxx) { 
    ggplot(dataset)+ 
    geom_violin(aes_string(y = var, x = groupcol), scale = "width", 
       alpha = 0.4, adjust = adjust1) + 
    geom_violin(data = dataset_orig, aes_(y = as.name(var), x = "Whole sample"), 
       scale = "width", alpha = .4, adjust = adjust1) + 
    scale_y_continuous(limits = c(0,ceiling(maxx)) , breaks = scales::pretty_breaks(15)) + 
    coord_flip() 
} 

violinplot_fun(dt1[n >= 3,], dataset_orig = dt1, "numbers", "name",0.5,100) 

はあなたにこれを与える:あなたは、個々の受け入れのためのしきい値を変更することはありません知っている場合にのみ、入力1に持っているので、

enter image description here

さらに、(すなわち、3)あなたは、このようなあなたの関数を書くことができデータセット引数:

violinplot_fun <- function(dataset, var, groupcol, adjust1, maxx) { 
    ggplot(dataset[n >= 3]) + 
    geom_violin(aes_string(y = var, x = groupcol), scale = "width", 
      alpha = 0.4, adjust = adjust1) + 
    geom_violin(data = dataset, aes_(y = as.name(var), x = "Whole sample"), scale = "width", 
      alpha = .4, adjust = adjust1) + 
    scale_y_continuous(limits = c(0,ceiling(maxx)) , breaks = scales::pretty_breaks(15)) + 
    coord_flip() 
} 

または、しきい値として引数を設定できます。

+0

私はそれを編集して、プロット自体が3つ以下の観測を含むカテゴリを除外し、データテーブルにはまだそれらが含まれています – tbradley

+0

私は今あなたの質問をよりよく理解していると思います。私は、あなたが望むものを得るためにあなたの機能に若干の調整を加えました – tbradley

関連する問題