1

我知道这有点混乱,但我正在尝试进一步划分构成堆叠条形图的数据。这是到目前为止的样子:

A = ggplot(data=yield,aes(N,Mean.Yield,fill=Cutting))
B=A+facet_grid(Location~Mngmt)+geom_bar(stat="identity")
B+labs(x="Nitrogen Level")+labs(y="Yield (lb/acre)")

生成此图:(我会发布该图,但显然我的名誉达不到新成员的水平!)

如何按“物种”因子进一步划分条形?我假设它涉及添加另一个geom,但我对这一切都很陌生。谢谢!

编辑添加:尝试mtcars用于虚拟数据,虽然不是最好的,因为 mpg 不像我的数据中的两次切割那样的产量。

mtcars$cyl=as.factor(mtcars$cyl)
mtcars$vs=as.factor(mtcars$vs)
mtcars$am=as.factor(mtcars$am)
mtcars$gear=as.factor(mtcars$gear)
mtcars$carb=as.factor(mtcars$carb)
A = ggplot(data=mtcars,aes(cyl,mpg,fill=gear))
B=A+facet_grid(am~vs)+geom_bar(stat="identity")

这会产生这个丑陋的图表: http: //i.imgur.com/sK7A5am.png ( http://i.imgur.com/sK7A5am.png ) 我希望将这些条形中的每一个(例如cylinders)分成两个并排的条形图(在本例中,6 个并排的条形图表示发动机的 mpg,carb每个气缸系数的水平各不相同)。我希望这是有道理的。再次感谢!

4

1 回答 1

1

好的,根据您的评论,我认为您想position更改geom_bar(). 使用diamonds来自 的数据集ggplot2,这看起来像您想要的吗?

library(ggplot2)
## note the diamonds dataset comes with ggplot2

ggplot(diamonds, aes(clarity, fill=cut)) + 
    geom_bar(position="dodge") 

来自ggplot2的示例图像
(来源:ggplot2.org

然后,您只需添加您的facet和其他详细信息。举个diamonds例子,这将是

ggplot(diamonds, aes(clarity, fill=cut)) + 
    geom_bar(position="dodge") + 
    facet_grid(color ~ clarity)

我想出了如何浏览ggplot2 帮助文件

于 2015-01-06T23:46:57.473 回答