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)