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.
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.
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.
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>.
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.
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()
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))))
Notably, you wouldn't have to do names(df)[1] = 'x'
because you remove the names with unname
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With