0

Say you have two dataframes

M1 <- data.frame(sample(1:3, 500, replace = TRUE), ncol = 5)
M2 <- data.frame(sample(1:3, 500, replace = TRUE), ncol = 5)

and I want to overlay them as violin plots as seen here: Overlay violin plots ggplot2

but I have 2 dataframes like above (but bigger) not one with 3 columns as in the example above

I have tried the advice using melt as seen here: Violin plot of a data frame

but I cant get it to overlay two dataframes

help is much appreciated:

Community
  • 1
  • 1
StudentOfScience
  • 809
  • 3
  • 11
  • 35

1 Answers1

2

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"))

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thank you! that works, just a thing I had to look up, adding (" + theme(axis.text.x = element_text(angle = 90, hjust = 1)) ") will make the x-axis text not get scrunched, which was the case for me. – StudentOfScience Apr 17 '14 at 00:30
  • I chose the first option it makes more sense for what I am doing. But I want a color legend, how do you do that with the first method? thanks – StudentOfScience Apr 17 '14 at 02:35