Like this?

library(ggplot2)
library(reshape2)
set.seed(1)
M1 <- data.frame(matrix(sample(1:5, 500, replace = TRUE), ncol = 5))
M2 <- data.frame(matrix(sample(2:4, 500, replace = TRUE), ncol = 5))
M1.melt <- melt(M1)
M2.melt <- melt(M2)
ggplot() +
geom_violin(data=M1.melt, aes(x=variable,y=value),fill="lightblue",colour="blue")+
geom_violin(data=M2.melt, aes(x=variable,y=value),fill="lightgreen",colour="green")
There are several issues. First, data.frame(...)
does no take an ncol
argument, so your code just generates a pair of 2-column data frames with the second column called ncol
with all values = 5. If you want 5 columns (do you??) then you have to use matrix(...)
as above.
Second, you do need to use melt(...)
to reorganize the dataframes from "wide" format (categories in 5 different columns) to "long" format (all data in 1 column, called value
, with categories distinguihsed by a second column, called variable
).
Another way to do this combines the two dataframes first:
M3 <- rbind(M1,M2)
M3$group <- rep(c("A","B"),each=100)
M3.melt <- melt(M3, id="group")
ggplot(M3.melt, aes(x=variable, y=value, fill=group)) +
geom_violin(position="identity")

Note that this generates a slightly different plot because ggplot
scales the width of the violins together, whereas in the earlier plot they were scaled separately.
EDIT (Response to OP's comment)
To put the fill colors in a legend, you have to make them part of an aesthetic scale: put fill=...
inside the call to aes(...)
as follows.
ggplot() +
geom_violin(data=M1.melt, aes(x=variable,y=value,fill="M1"),colour="blue")+
geom_violin(data=M2.melt, aes(x=variable,y=value,fill="M2"),colour="green")+
scale_fill_manual(name="Data Set",values=c(M1="lightblue",M2="lightgreen"))
