当我使用以下代码时,标准 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 的四分位数。
最佳答案
什么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
关于r - 使用 ggplot2 在一个图上绘制多个箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847506/