简体   繁体   English

R中多列的箱线图

[英]Boxplot across multiple columns in R

all,全部,

Pretty new to R so I'm having some trouble trying to figure this out. R 非常新,所以我在尝试解决这个问题时遇到了一些麻烦。 This is a sample of the data I'm working with...这是我正在处理的数据示例...

ID ID Cross_specific交叉特定 Cross_pop Cross_pop seed_count种子数
1 1 interspecific种间 NA不适用 # #
2 2 intraspecific种内 intrapopulation种群内 # #
3 3 intraspecific种内 interpopulation种群间 # #

What I want to do is create a boxplot that separates the seed count by intraspecific/intrapopulation, intraspecific/interpopulation, and interspecific - so three separate boxplots.我想要做的是创建一个箱线图,通过种内/种群内、种内/种群间和种间分离种子计数 - 所以三个单独的箱线图。 I'm pretty sure I'll have to get the boxplot to read across two different columns, which I think I've figured out.我很确定我必须让箱线图读取两个不同的列,我想我已经弄清楚了。

The problem is it separates the boxplot into five categories: "interspecific" and "intraspecific" from the "Cross_specific" column as well as "NA", "intrapopulation", and "interpopulation" from the "Cross_pop_ column.问题在于它将箱线图分为五类:“Cross_specific”列中的“interspecific”和“intraspecific”,以及“Cross_pop_列”中的“NA”、“intrapopulation”和“interpopulation”。

Is there a way to take the "intra" and "inter" categories (leaving the "NA" out) from the "Cross_pop" column and only the "interspecific" category from the "Cross_specific" column?有没有办法从“Cross_pop”列中取出“intra”和“inter”类别(将“NA”排除在外),而只从“Cross_specific”列中取出“interspecific”类别?

This is the line I used这是我使用的线路

boxplot(seed_count_avg~cross_pop+cross_specific="interspecific",data=data,main="Seed Count",xlab="Cross Type",ylab="Seed Count")

Unfortunately, I got an error ( Error: unexpected '=' in "boxplot(seed_count_avg~cross_pop+cross_specific=" , in case you're wondering).不幸的是,我遇到了一个错误(错误:“boxplot(seed_count_avg~cross_pop+cross_specific="中的意外 '=' ,以防万一)。

I also used this code我也用过这个代码

boxplot(seed_count_avg~cross_pop+cross_specific,data=data,main="Seed Count",xlab="Cross Type",ylab="Seed Count")

Which created boxplots, but with empty spaces.它创建了箱线图,但有空格。

Thank you in advance!先感谢您!

This might be close to what you are trying to do.这可能与您正在尝试做的很接近。 First we need reproducible data:首先我们需要可重现的数据:

set.seed(42)
Cross_specific <- sample(c("interspecific", "intraspecific"), 100, replace=TRUE)
Cross_pop <- sample(c("intrapopulation", "interpopulation"), 100, replace=TRUE)
Cross_pop[Cross_specific=="interspecific"] <- ""
seed_count <- sample(1000, 100)
data <- data.frame(Cross_specific, Cross_pop, seed_count)

Whenever Cross_specific=="interspecific" we set the value of Cross_pop to "", not NA because this is not missing data.每当Cross_specific=="interspecific"我们将Cross_pop的值设置为 "",而不是NA因为这不是缺失数据。 Now the boxplot:现在箱线图:

boxplot(seed_count~Cross_specific+Cross_pop, data=data2, main="Seed Count", 
     xlab="Cross Type", ylab="Seed Count", drop=TRUE, cex.axis=.9)

Which produces the following plot:这产生以下情节:

箱形图

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

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