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)
答案 0 :(得分:0)
您可以添加第二个geom_violin
图层,其中包含固定的x
值,而不是变量。
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)