使用ggplot2中的x列名从数据框的所有列构建箱形图 [英] Building a box plot from all columns of data frame with column names on x in ggplot2

查看:307
本文介绍了使用ggplot2中的x列名从数据框的所有列构建箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下结构的数据框 my.df

  ABC 
1 1 1 2
2 2 3 4
3 3 5 6
4 NA 7 8
5 NA 9 NA

如何使用x轴上的列名和y上的所有值来构建箱形图?



有很多答案:

  ggplot(melt(my.df),aes(变量,值))+ geom_boxplot()

但我不明白,我实际上应该通过作为可变和价值。我尝试了 x = colnames(my.df)),这部分工作,但我仍然不知道如何处理y。


<您可以使用 stack 来转换数据框:
$ b $
b

  library(ggplot2)
ggplot(stack(df),aes(x = ind,y = values))+
geom_boxplot()


I have a data frame my.df of the following structure:

   A B  C
1  1 1  2
2  2 3  4
3  3 5  6
4 NA 7  8
5 NA 9 NA

How to build a box plot from it with column names on x axis and all the values on y?

There are many answers like:

ggplot(melt(my.df), aes(variable, value)) + geom_boxplot()

But I don't understand, what I actually should pass as "variable" and "value". I tried x=colnames(my.df)) and this partially works, however I still have no idea what to do with y.

解决方案

You can use stack to transform the data frame:

library(ggplot2)
ggplot(stack(df), aes(x = ind, y = values)) +
  geom_boxplot()

这篇关于使用ggplot2中的x列名从数据框的所有列构建箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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