使用ggplot2绘制多个箱形图 [英] Multiple boxplots on one plot with ggplot2
问题描述
boxplot(丰度[四分位数== 1 ]〜Year [Quartile == 1],col =LightBlue,main =Quartile1(Rare))
我想在ggplot2中生成类似的东西。到目前为止,我使用这个:
d1 = data.frame(x = data $ Year [Quartile == 1], y =数据$丰度[四舍五入== 1])$ b $ ba < - ggplot(d1,aes(x,y))
a + geom_boxplot()
有30年的数据。每年有145种。在每一年,145种被分为1-4的四分位数。
然而,我只用一个盒子获得一个盒子。任何想法如何获得30个boxlot(沿x轴每年一个)?任何帮助非常感谢。有30年的数据。每年有145种。每年有145种物种被分为四至四分之一。
str(d1)
告诉你关于 X
?如果是数字或整数,那么这可能是你的问题。如果年
是一个因素,那么您将获得每年的箱型图。举个例子:pre $ library $ g
$一些玩具数据
df<< ; - data.frame(Year = rep(c(1:30),each = 20),Value = rnorm(600))
str(df)
pre>
请注意, Year
是一个整数变量
Standard R plotting produces 30 boxplots in one plot when I use this code:
boxplot(Abundance[Quartile==1]~Year[Quartile==1],col="LightBlue",main="Quartile1 (Rare)")
I would like to produce something similar in ggplot2. So far i'm using this:
d1 = data.frame(x=data$Year[Quartile==1],y=data$Abundance[Quartile==1])
a <- ggplot(d1,aes(x,y))
a + geom_boxplot()
There are 30 years of data. In each year there are 145 species. In each year the 145 species are categorized into quartiles of 1-4.
However, I'm only getting a single boxplot using this. Any idea how to get 30 boxplots (one for each year) along the x axes? Any help much appreciated.
There are 30 years of data. In each year there are 145 species. In each year the 145 species are categorized into quartiles of 1-4.
What does str(d1)
tell you about x
? If numeric or integer, then that could be your problem. If Year
is a factor, then you get a boxplot for each Year. As an example:
library(ggplot2)
# Some toy data
df <- data.frame(Year = rep(c(1:30), each=20), Value = rnorm(600))
str(df)
Note that Year
is an integer variable
ggplot(df, aes(Year, Value)) + geom_boxplot() # One boxplot
ggplot(df, aes(factor(Year), Value)) + geom_boxplot() # 30 boxplots
这篇关于使用ggplot2绘制多个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!