I have a data frame with 43 different numeric variables and one categorical variable with 823 rows(patients). I need a violin plot for each numeric variable(each column) grouped based on the categorical variable.The categorical variable has different ethnic groups like "AA","Asian","Native Americans". So I need the 43 violin plots showing the distribution of the each numeric variable(i.e each column) based on the different ethnic groups.The categorical variable would be constant.I am trying to use for loops but I think the code has a problem.It is not working.
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()
}