简体   繁体   中英

R violin plot overlay 2 dataframes

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM