简体   繁体   English

R中的多个变量箱形图

[英]Multiple Variable Boxplots in R

I am new to R and statistical analysis as a whole. 我是R和整体统计分析的新手。 I am currently working with a data set that includes age and memory as well as concentrations of specific compounds. 我目前正在使用一个数据集,其中包括年龄和记忆力以及特定化合物的浓度。 My data was obtained from a csv file by using read.csv. 我的数据是使用read.csv从csv文件获得的。

Currently I can display a box plot for one age group and one protein by using 目前,我可以使用来显示一个年龄段和一种蛋白质的箱形图

boxplot(data$compound_A[data$Age.Code==3]~q$Memory.Code[q$Age.Code==3])

I encounter a problem when I want to look at two age groups. 当我想看两个年龄段的人时遇到问题。 I have tried 我努力了

boxplot(data$compound_A[data$Age.Code==3]~q$Memory.Code[data$Age.Code==3],
data$compound_A[data$Age.Code==2]~q$Memory.Code[q$Age.Code==2])

and a few permutations of it such as: 以及它的一些排列,例如:

boxplot(data$compound_A[data$Age.Code==3]~q$Memory.Code[data$Age.Code==3],data,
data$compound_A[data$Age.Code==2]~q$Memory.Code[q$Age.Code==2],data)

Unfortunately none of these approaches has worked. 不幸的是,这些方法都不起作用。 Any help would be appreciated! 任何帮助,将不胜感激!

Thank you! 谢谢!

As suggested here is some of my data using the dput option 如建议的那样,这是我使用dput选项的一些数据

structure(list(ID = c(635L, 637L, 638L, 639L, 641L, 642L, 644L, 
646L, 647L, 649L, 652L, 676L, 677L, 678L, 679L, 682L, 684L, 686L, 
688L, 692L, 693L, 715L, 716L, 717L, 718L, 719L), Age.Code = c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L), Memory.Code = c(2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), Compound_A = c(NA, 0, 93.25, 
42.79, 148.94, 41.98, 38.99, 0, 0, 42.79, 41.98, 0, 27.38, 76.51, 
121.6, 0, 153.69, 68.6, 189.15, NA, 210.73, 0, 27.38, 2.12, 76.51, 
76.51)), .Names = c("ID", "Age.Code", "Memory.Code", "Compound_A"
), class = "data.frame", row.names = c(NA, -26L))

Here's a ggplot solution. 这是一个ggplot解决方案。

library(ggplot2)
ggplot(data, aes(x=factor(Memory.Code),y=Compound_A))+
  geom_boxplot(aes(fill=factor(Age.Code)),position=position_dodge(.9))+
  scale_fill_discrete(name="Age.Code")+labs(x="Memory.Code")

Here's the way using base R. 这是使用基数R的方法。

boxplot(Compound_A~factor(Age.Code)+factor(Memory.Code),data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM