本文介绍了使用ggplot facet_grid散布相同变量在不同条件下的散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想关联具有不同行值的点的数据帧的同一列。例如,在 iris 数据框中,我想做三个散点图,比较 Petal.Length of c code> versicolor , setosa with c $ c> virginica > virginica 和 versicolor 与 setosa 。我希望它看起来就像普通的 facet_grid 或 facet_wrap 图。例如,我可以这样做: ggplot(iris)+ geom_point(aes(x = Petal.Length,y = Petal.Length)) + facet_grid(〜物种) 这不是我想要的,因为它绘制了 Petal.Length ,但我希望情节是这样出现的,除非我手动编码哪些物种与其他物种进行比较。这怎么能在 ggplot 中完成?谢谢。解决方案最好先将数据分组。我会做这样的事情: #分别为每个物种获取Petal.Length df1< - 子集(虹膜,Species ==virginica,select = c(Petal.Length,Species)) df2 df3< - subset(iris,Species ==setosa,select = c(Petal.Length,Species)) #构建物种1对2,2对3和3 vs 1 data df y = c(df2 $ Petal。长度,df3 $ Petal.Length,df1 $ Petal.Length), grp = rep(c(virginica.versicolor,versicolor.setosa,setosa.virginica),each = 50)) df $ grp< - factor(df $ grp) #plot require(ggplot2) ggplot(data = df,aes(x = x,y = y))+ geom_point(aes(color = grp))+ facet_wrap(〜grp) 结果在: I'd like to correlate the same column of a dataframe for points with distinct row values. For example, in the iris dataframe, I'd like to make three scatter plots comparing Petal.Length of virginica with that of versicolor, setosa with virginica and versicolor with setosa. I want it to appear just like a normal facet_grid or facet_wrap plot. For example, I can do:ggplot(iris) + geom_point(aes(x=Petal.Length, y=Petal.Length)) + facet_grid(~Species)This is not what I want, since it's plotting Petal.Length of each species against itself, but I want the plot to appear like this, except where I handcode which species to compare to what other species. How can this be done in ggplot? Thanks. 解决方案 It is better to group the data first. I'd do something like this:# get Petal.Length for each species separately df1 <- subset(iris, Species == "virginica", select=c(Petal.Length, Species))df2 <- subset(iris, Species == "versicolor", select=c(Petal.Length, Species))df3 <- subset(iris, Species == "setosa", select=c(Petal.Length, Species))# construct species 1 vs 2, 2 vs 3 and 3 vs 1 datadf <- data.frame(x=c(df1$Petal.Length, df2$Petal.Length, df3$Petal.Length), y = c(df2$Petal.Length, df3$Petal.Length, df1$Petal.Length), grp = rep(c("virginica.versicolor", "versicolor.setosa", "setosa.virginica"), each=50))df$grp <- factor(df$grp)# plotrequire(ggplot2)ggplot(data = df, aes(x = x, y = y)) + geom_point(aes(colour=grp)) + facet_wrap( ~ grp)This results in: 这篇关于使用ggplot facet_grid散布相同变量在不同条件下的散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 09:11