4

当我使用此代码时,标准 R 绘图在一个绘图中生成 30 个箱线图:

boxplot(Abundance[Quartile==1]~Year[Quartile==1],col="LightBlue",main="Quartile1 (Rare)")

我想在 ggplot2 中产生类似的东西。到目前为止,我正在使用这个:

d1 = data.frame(x=data$Year[Quartile==1],y=data$Abundance[Quartile==1])
a <- ggplot(d1,aes(x,y))
a + geom_boxplot()

有30年的数据。每年有145种。每年有 145 个物种被分类为 1-4 的四分位数。

但是,我只得到一个使用它的箱线图。知道如何沿 x 轴获得 30 个箱线图(每年一个)吗?非常感谢任何帮助。

有30年的数据。每年有145种。每年有 145 个物种被分类为 1-4 的四分位数。

4

1 回答 1

9

str(d1)告诉你什么x?如果是数字或整数,那么这可能是您的问题。如果Year是一个因素,那么你会得到一个每年的箱线图。举个例子:

library(ggplot2)

# Some toy data
df <- data.frame(Year = rep(c(1:30), each=20), Value = rnorm(600))
str(df)

请注意,这Year是一个整数变量

ggplot(df, aes(Year, Value)) + geom_boxplot()   # One boxplot

ggplot(df, aes(factor(Year), Value)) + geom_boxplot()   # 30 boxplots
于 2012-06-01T11:19:36.300 回答