r - ggplot2中的两列分组

标签 r ggplot2

是否可以按两列分组?于是叉积就画出来了 通过geom_point()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()) # ...

这样点612共享一个组,但不与3共享。

最佳答案

this question 为例,使用交互将两列组合成一个新因子:

# Data frame with two continuous variables and two factors 
set.seed(0)
x <- rep(1:10, 4)
y <- c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm(20)/5)
treatment <- gl(2, 20, 40, labels=letters[1:2])
replicate <- gl(2, 10, 40)
d <- data.frame(x=x, y=y, treatment=treatment, replicate=replicate)

ggplot(d, aes(x=x, y=y, colour=treatment, shape = replicate,
  group=interaction(treatment, replicate))) + 
  geom_point() + geom_line()

ggplot example

关于r - ggplot2中的两列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968976/

相关文章:

r - 在 rmarkdown 中隐藏代码

r - gganimate 和(有时)空方面的问题

r - ggplot2条形图的水平线

r - 将分类数据添加到 XY 图

html - 如何防止 splitLayout、Shiny、R 中两个输入标签重叠?

r - 在多个表达式上使用 eval(substitute())

r - 使用 ggmap 和 ggplot2 获取带有点的 map

r - 在ggplot2中更改颜色填充顺序

r - 在R图中的x轴下方添加箭头

r - 如何使用 glmnet 包提取最佳 lambda 的 CV 错误?