programing

ggplot2의 두 열로 그룹화

telebox 2023. 6. 16. 21:40
반응형

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()) # ...

그 점들이6그리고.12그룹을 공유하지만 그룹과 공유하지 않음3.

이 질문에서 예를 들어, 사용interaction두 열을 새 요인으로 결합하는 방법

# 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

예:

 qplot(round, price, data=firm, group=id, color=id, geom='line') +  
      geom_smooth(aes(group=interaction(size, type)))

왜 그냥paste두 개의 열을 함께 사용하고 그 변수를 그룹으로 사용합니까?

frame$grp <- paste(frame[,1],frame[,2])

이를 위한 좀 더 공식적인 방법은 기능을 사용하는 것입니다.interaction.

언급URL : https://stackoverflow.com/questions/9968976/group-by-two-columns-in-ggplot2

반응형