简体   繁体   English

使用ggplot2的箱线图

[英]Boxplots using ggplot2

I am completely new to using ggplot2 but heard of it's great plotting capabilities. 我对使用ggplot2完全陌生,但听说它具有出色的绘图功能。 I have a list with of different samples and for each sample observations according to three instruments. 我有一个包含不同样本的列表,并且根据三种仪器针对每个样本观察结果。 I would like to turn that into a figure with boxplots. 我想把它变成带有箱线图的人物。 I cannot include a figure but the code to make an example figure is included below. 我无法包含图形,但是下面包含了用于创建示例图形的代码。 The idea is to have for each instrument a figure with boxplots for each sample. 想法是为每个仪器配备一个带有每个样本盒图的图形。

In addition, next to the plots I would like to make a sort of legend giving a name to each of the sample numbers. 此外,在图旁边,我想做一个图例,为每个样本编号命名。 I have no idea on how to start doing this with ggplot2. 我不知道如何开始使用ggplot2。

Any help will be appreciated 任何帮助将不胜感激

The R-code to produce the example image is: 产生示例图像的R代码为:

#Make data example
Data<-list();
Data$Sample1<-matrix(rnorm(30),10,3);
    Data$Sample2<-matrix(rnorm(30),10,3);
Data$Sample3<-matrix(rnorm(30),10,3);
    Data$Sample4<-matrix(rnorm(30),10,3);

#Make the plots
par(mfrow=c(3,1)) ;
boxplot(data.frame(Data)[seq(1,12,by=3)],names=c(1:4),xlab="Sample number",ylab="Instrument 1");
boxplot(data.frame(Data)[seq(2,12,by=3)],names=c(1:4),xlab="Sample number",ylab="Instrument 2");
boxplot(data.frame(Data)[seq(3,12,by=3)],names=c(1:4),xlab="Sample number",ylab="Instrument 3");

First, you'll want to set your data up differently: as a data.frame rather than a list of matrices. 首先,您需要以不同的方式设置数据:作为data.frame而不是矩阵列表。 You want one column for sample , one column for instrument , and one column for the observed value . 您需要一栏作为sample ,一栏作为instrument ,一栏作为观测value Here's a fake dataset: 这是一个伪数据集:

df <- data.frame(sample = rep(c("One","Two","Three","Four"),each=30), 
                 instrument = rep(rep(c("My Instrument","Your Instrument","Joe's Instrument"),each=10),4),
                 value = rnorm(120))

> head(df)
  sample    instrument       value
1    One My Instrument  0.08192981
2    One My Instrument -1.11667766
3    One My Instrument  0.34117450
4    One My Instrument -0.42321236
5    One My Instrument  0.56033804
6    One My Instrument  0.32326817

To get three plots, we're going to use faceting . 为了获得三个图,我们将使用faceting To get boxplots we use geom_boxplot . 为了获得geom_boxplot图,我们使用geom_boxplot The code looks like this: 代码如下:

ggplot(df, aes(x=sample,y=value)) + 
  geom_boxplot() + 
  facet_wrap(~ instrument, ncol=1)

在此处输入图片说明

Rather than including a legend for the sample numbers, if you put the names directly in the sample variable it will print them below the plots. 如果不直接在样本变量中包含图例,则将名称直接放在样本变量中,它将在图表下方显示它们。 That way people don't have to reference numbers to names: it's immediately clear what sample each plot is for. 这样,人们就不必在名称上引用数字了:可以立即清楚地知道每个图用于什么样本。 Note that ggplot puts the factors in alphabetical order by default; 请注意,默认情况下,ggplot将因子按字母顺序放置; if you want a different ordering you have to change it manually. 如果要更改其他顺序,则必须手动更改。

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

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