如何为刻面添加不同的线条 [英] How to add different lines for facets
问题描述
我有数据可以查看两种不同物种的单一栽培和混合栽培之间的生长差异.此外,我制作了一个图表来使我的数据清晰.
I have data where I look at the difference in growth between a monoculture and a mixed culture for two different species. Additionally, I made a graph to make my data clear.
我想要一个带有误差条的条形图,整个数据集当然更大,但对于这个图形,这是带有条形图均值的 data.frame
.
I want a barplot with error bars, the whole dataset is of course bigger, but for this graph this is the data.frame
with the means for the barplot.
plant species means
Mixed culture Elytrigia 0.886625
Monoculture Elytrigia 1.022667
Monoculture Festuca 0.314375
Mixed culture Festuca 0.078125
根据这些数据,我在 ggplot2
中绘制了一个图表,其中 plant
在 x 轴上,means
在 y 轴上,我用一个方面来划分物种.
With this data I made a graph in ggplot2
, where plant
is on the x-axis and means
on the y-axis, and I used a facet to divide the species.
这是我的代码:
limits <- aes(ymax = meansS$means + eS$se, ymin=meansS$means - eS$se)
dodge <- position_dodge(width=0.9)
myplot <- ggplot(data=meansS, aes(x=plant, y=means, fill=plant)) + facet_grid(. ~ species)
myplot <- myplot + geom_bar(position=dodge) + geom_errorbar(limits, position=dodge, width=0.25)
myplot <- myplot + scale_fill_manual(values=c("#6495ED","#FF7F50"))
myplot <- myplot + labs(x = "Plant treatment", y = "Shoot biomass (gr)")
myplot <- myplot + opts(title="Plant competition")
myplot <- myplot + opts(legend.position = "none")
myplot <- myplot + opts(panel.grid.minor=theme_blank(), panel.grid.major=theme_blank())
到目前为止一切正常.但是,我想在两个方面添加两条不同的水平线.为此,我使用了以下代码:
So far it is fine. However, I want to add two different horizontal lines in the two facets. For that, I used this code:
hline.data <- data.frame(z = c(0.511,0.157), species = c("Elytrigia","Festuca"))
myplot <- myplot + geom_hline(aes(yintercept = z), hline.data)
但是,如果我这样做,我会得到一个有两个额外方面的图,其中绘制了两条水平线.相反,我希望将水平线绘制在带有条形的刻面中,而不是制作两个新的刻面.任何人都知道如何解决这个问题.
However if I do that, I get a plot were there are two extra facets, where the two horizontal lines are plotted. Instead, I want the horizontal lines to be plotted in the facets with the bars, not to make two new facets. Anyone a idea how to solve this.
我认为如果我把我现在创建的图表放得更清楚:
I think it makes it clearer if I put the graph I create now:
推荐答案
确保两个数据集中的变量种类相同.如果它是其中一个的因素,那么它也一定是另一个的因素
Make sure that the variable species is identical in both datasets. If it a factor in one on them, then it must be a factor in the other too
library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))
dummy2$X <- factor(dummy2$X)
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))
这篇关于如何为刻面添加不同的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!