本文介绍了如何为facet添加不同的线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有数据可以看到两种不同物种的单一栽培和混合栽培之间的增长差异。此外,我制作了一张图表,以清晰显示我的数据。 我想要一个包含错误栏的barplot,整个数据集当然会更大,但对于此图,这是 data.frame 与barplot的手段。 植物物种意味着混血文化Elytrigia 0.886625 Monoculture Elytrigia 1.022667 Monoculture Festuca 0.314375 混合文化费斯图卡0.078125 有了这些数据,我在 ggplot2 ,其中 plant 位于x轴上,指y轴上的 ,我用一个方面来划分物种。 这是我的代码: 限制< - aes( ymax = meansS $ means + eS $ se,ymin = meansS $ means - eS $ se) dodge 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 =植物处理,y =拍摄生物量(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()) 目前为止没有问题。但是,我想在两个方面添加两条不同的水平线。为此,我使用了以下代码: hline.data< - data.frame(z = c(0.511,0.157) ,物种= c(Elytrigia,Festuca)) myplot< - myplot + geom_hline(aes(yintercept = z),hline.data) pre> 然而,如果我这样做,我会得到一个情节是有两个额外的方面,其中两条水平线被绘制。相反,我希望水平线被绘制在带有横条的小平面上,而不是创造出两个新的方面。任何人都知道如何解决这个问题。 我认为如果我把我现在创建的图表更清晰: 解决方案确保两个数据集中的可变物种是相同的。如果它是其中的一个因素,那么它也必须是另一个因素。 library(ggplot2) dummy1< - expand.grid(X = factor(c(A,B)),Y = rnorm(10)) dummy1 $ D ggplot(dummy1,aes(x = D,y = Y ))+ geom_point()+ facet_grid(〜X)+ geom_hline(data = dummy2,aes(yintercept = Z)) dummy2 $ X ggplot(dummy1,aes(x = D,y = Y))+ geom_point()+ facet_grid (〜X)+ geom_hline(data = dummy2,aes(yintercept = Z)) 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.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 meansMixed culture Elytrigia 0.886625Monoculture Elytrigia 1.022667Monoculture Festuca 0.314375Mixed culture Festuca 0.078125With 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.This is my code: 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 toolibrary(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)) 这篇关于如何为facet添加不同的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 07:55