2017-03-09 6 views
0

ggplotはグループ化変数を使用せずにサブプロットを追加して、図のようにサンプル全体を表示できますか?データを複製せずにサンプル全体のサブプロットを追加します

# example df 
b<-abs(round(rnorm(500, 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, 500, replace=TRUE, prob=pr) 
df<-data.frame(name=x,numbers=b) 

#####replace this with something in the ggplot 
dfdupli<-df 
dfdupli$name<-"Whole sample" 
dfrepeated<-rbind(df,dfwhole) 
###### 

library(ggplot2) 
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) + 
    scale_y_continuous(limits = c(0,ceiling(maxx)) , breaks=pretty_breaks(15)) + 
    coord_flip() 
} 

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

enter image description here

答えて

0

あなたは、固定x値の代わりに変数を使用して二geom_violin層を追加することができます。

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

関連する問題