团体票吗? [英] Boxplots for groups?

查看:59
本文介绍了团体票吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据集(测试):

 类型Met1 Met2 Met3 Met4 
TypeA 65 43 97 77
TypeA 46 25 76 77
TypeA 44 23 55 46
TypeA 46 44 55 77
TypeA 33 22 55 54
TypeB 66 8 66 47
Type B 55 76 66 65
TypeB 55 77 88 46
TypeB 36 67 55 44
TypeB 67 55 76 65

我检查了箱形图中的许多链接,但对于想要的箱形图类型,我仍然没有成功。我希望有一个箱形图,我的X轴对所有大都会(Met1,Met2,Met3,Met4)都具有类型A(黄色,橙色)。本质上,我想要以下内容(摘自



我正在尝试类似

  boxplot(公式= len〜类型,数据=测试,子集== TypeA)
boxplot(公式= len〜类型,数据=测试,子集== TypeA,add = TRUE)
图例(legend = c( typeA, typeB),fill = c( yellow, orange))

但是我无法使用其中的任何一个。谁能帮助我知道如何以正确的方式在测试数据上绘制此类箱形图?

解决方案

具有 ggplot2 的解决方案。



首先,使用 melt 将数据框 test 转换为长格式:

 库(reshape2)
test.m<-熔化(测试)

绘制数据:

  library(ggplot2 )
ggplot(test.m,aes(x =变量,y =值,填充=类型))+
geom_boxplot()+
scale_fill_manual(values = c( yellow,橙色))


I have a dataset (test) as given below:

Type    Met1    Met2    Met3    Met4
TypeA   65  43  97  77
TypeA   46  25  76  77
TypeA   44  23  55  46
TypeA   46  44  55  77
TypeA   33  22  55  54
TypeB   66  8   66  47
TypeB   55  76  66  65
TypeB   55  77  88  46
TypeB   36  67  55  44
TypeB   67  55  76  65

I have checked a lot of links on box plots, but I still have not succeeded for the type of box plot I want. I wish to have a boxplot with my X-axis having type A (yellow, orange) for all the Mets (Met1, Met2, Met3, Met4). In essence, I want something like the following (taken from here):

I am trying somethings like,

boxplot(formula = len ~ Type , data = test, subset == "TypeA")
boxplot(formula = len ~ Type , data = test, subset == "TypeA", add=TRUE)
Legend(legend = c( "typeA", "typeB" ), fill = c( "yellow", "orange" ) )

But I am not able to work it out with any of it. Can anyone help me know how do I make such box plots on my test data in the corrected way?

解决方案

A solution with ggplot2.

First, transform your data frame test to the long format using melt:

library(reshape2)
test.m <- melt(test)

Plot the data:

library(ggplot2)
ggplot(test.m, aes(x = variable, y = value, fill = Type)) +
  geom_boxplot() +
  scale_fill_manual(values = c("yellow", "orange"))

这篇关于团体票吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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