数据框的小提琴图 [英] Violin plot of a data frame

查看:82
本文介绍了数据框的小提琴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame,例如:

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

我接下来要做的是将 df 的列名绘制为 x 轴标签而不是 vioplot 的默认 x 轴标签,此外还有一个这样他们就不会互相碾压.我想这可以通过在图中展开 df 的列或倾斜 x 轴标签来实现.但我想不通.

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.

推荐答案

可能更容易使用 ggplot

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

需要将数据转换成ggplot可以处理的东西:

Need to transform the data into something ggplot can handle:

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

和情节:

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

这篇关于数据框的小提琴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆