在 ggplot2 中按两列分组 [英] group by two columns in ggplot2
本文介绍了在 ggplot2 中按两列分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以按两列分组?所以绘制叉积通过 geom_point()
和 geom_smooth()
?
Is it possible to group by two columns? So the cross product is drawn
by geom_point()
and geom_smooth()
?
例如:
frame <- data.frame(
series <- rep(c('a', 'b'), 6),
sample <- rep(c('glass','water', 'metal'), 4),
data <- c(1:12))
ggplot(frame, aes()) # ...
这样 6
和 12
点共享一个组,但不与 3
共享.
Such that the points 6
and 12
share a group, but not with 3
.
推荐答案
为什么不将这两列paste
放在一起并使用那个 变量作为组?
Why not just paste
those two columns together and use that variable as groups?
frame$grp <- paste(frame[,1],frame[,2])
一种更正式的方法是使用函数interaction
.
A somewhat more formal way to do this would be to use the function interaction
.
这篇关于在 ggplot2 中按两列分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文