Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Violin plot of a data frame

Tags:

plot

r

I have a data.frame, for example:

df = data.frame(AAA=rnorm(100,1,1),BBB=rnorm(100,2,1.5),CCC=rnorm(100,1.5,1.2))

And I'd like to plot each of its columns in a joint violin plot.

Here's where I'm at so far:

names(df)[1] = 'x'
do.call('vioplot', c(df,col="red",drawRect=FALSE))

What I want to do next is to plot the colnames of df as x-axis labels rather than the default x-axis labels of vioplot and in addition in a way that they don't run over each other. I imagine this can be achieved either by spreading the columns of df in the plot or by slanting the x-axis labels. But I can't figure that out.

like image 364
user1701545 Avatar asked Feb 22 '14 21:02

user1701545


People also ask

What does a violin plot show you?

What is a violin plot? A violin plot is a hybrid of a box plot and a kernel density plot, which shows peaks in the data. It is used to visualize the distribution of numerical data. Unlike a box plot that can only show summary statistics, violin plots depict summary statistics and the density of each variable.

How do you plot a data frame?

We can plot a dataframe using the plot() method. But we need a dataframe to plot. We can create a dataframe by just passing a dictionary to the DataFrame() method of the pandas library.

How do you make a violin plot?

To make a violin plot in R you can use ggplot2 and the geom_violin() function. For example, if we have the dataframe dataF and want to create a violin plot of the two groups response times you can use the following code: <code>p <- ggplot(aes(Group, RT), data = dataF))</code>.

What is a violin plot in Python?

A violin plot plays a similar role as a box and whisker plot. It shows the distribution of quantitative data across several levels of one (or more) categorical variables such that those distributions can be compared.


2 Answers

Probably easier to use ggplot

df = data.frame(AAA=rnorm(100,1,1),
                BBB=rnorm(100,2,1.5),
                CCC=rnorm(100,1.5,1.2))

Need to transform the data into something ggplot can handle:

df.m <- reshape2::melt(df, id.vars = NULL)

and plot:

library(ggplot2)
ggplot(df.m, aes(x = variable, y = value)) + geom_violin()

enter image description here

like image 127
rawr Avatar answered Sep 20 '22 09:09

rawr


I like the ggplot solution the best, but here is how you would do it with do.call:

do.call(vioplot,c(unname(df),col='red',drawRect=FALSE,names=list(names(df))))

enter image description here

Notably, you wouldn't have to do names(df)[1] = 'x' because you remove the names with unname.

like image 33
nograpes Avatar answered Sep 19 '22 09:09

nograpes