我有一个数据框,其中包含43个不同的数字变量和一个具有823行(患者)的类别变量。我需要一个根据分类变量分组的每个数字变量(每列)的小提琴图。分类变量具有不同的族群,例如“ AA”,“亚洲”,“美国原住民”。所以我需要43个小提琴图来显示每个数字变量(即每列)基于不同种族的分布。分类变量将是常数。我试图用于循环,但我认为代码有问题。它不起作用。
The code I am using to generate each violin plot is as follows.
ggplot2.violinplot(data=violin_df, xName='Race',yName='Cluster.1',
addDot=TRUE, dotSize=0.8,
dotPosition="jitter", jitter=0.2)
Here is the reproducible example dataset and code I am using to
generate
install.packages("devtools")
library(devtools)
install_github("kassambara/easyGgplot2")
library(easyGgplot2)
data <- matrix(rnorm(150), nrow=15)
data <- as.data.frame(data)
data$groups <- c("group1","group1","group1","group1","group1",
"group2","group2","group2","group2","group2",
"group3","group3","group3","group3","group3")
data$groups <- as.factor(data$groups)
ggplot2.violinplot(data=data,xName="groups",yName="V1",
addDot=TRUE, dotSize=0.8,
dotPosition="jitter", jitter=0.2)
######I need the violin plots for all the 9 column variables in
####the similar manner
for (i in 1:ncol(data)) {
png(file = paste("var_", i, ".png", sep=""))
ggplot2.violinplot(data=data,xName="groups",yName="data[,i]",
addDot=TRUE, dotSize=0.8,
dotPosition="jitter", jitter=0.2)
dev.off()
}