2013-11-14 2 views
0

현재 그래프 상단에 다른 일련의 데이터를 플롯하고자합니다. 추가 데이터에는 facet_wraping에 사용되는 3 (out of 6) spp에 대한 정보 만 들어 있습니다.ggplot2에서 aes를 레이어하고자합니다.

다른 일련의 데이터는 현재 (동일한 데이터 파일에있는) 열입니다.

현재 그래프 :

ped.num <- ggplot(data, aes(ped.length, seeds.inflorstem)) 

ped.num + geom_point(size=2) + theme_bw() + facet_wrap(~spp, scales = "free_y") 

추가 계층은 다음과 같습니다 그들은 단지 약간 작은 값을 가지고 있기 때문에

aes(ped.length, seeds.filled) 

나는, 내가 같은 y 축을 사용하여 플롯 할 수 있어야한다고 생각한다. 이 레이어를 추가하려면 어떻게해야합니까?

+0

'+ geom_point (aes (ped.length, seeds.filled))'를 추가해 보셨습니까? – ialm

답변

2

@ialm의 솔루션은 정상적으로 작동해야하지만 geom_*에 코드를 읽기 쉽게하기 때문에 각각 aes 함수를 별도로 호출하는 것이 좋습니다.

ped.num <- ggplot(data) + 
       geom_point(aes(x=ped.length, y=seeds.inflorstem), size=2) + 
       theme_bw() + 
       facet_wrap(~spp, scales="free_y") + 
       geom_point(aes(x=ped.length, y=seeds.filled)) 
2

(당신은 예를 들어, 데이터를 포함하는 경우 당신은 항상 더 나은 답변을 얻을 것이다,하지만 난 어둠 속에서 촬영 할게요) 같은 data.frame에 두 개의 변수를 플롯 할 때문에

을, 그것은 ggplot로 공급하기 전에 데이터를 바꿀 아마 가장 쉬운 다음 seeds.filled 값은 해당 종의 측면에서만 음모를해야

library(reshape2) 

# Melting data gives you exactly one observation per row - ggplot likes that 
dat.melt <- melt(dat, 
       id.var = c("spp", "ped.length"), 
       measure.var = c("seeds.inflorstem", "seeds.filled") 
) 


# Plotting is slightly different - instead of explicitly naming each variable, 
# you'll refer to "variable" and "value" 
ggplot(dat.melt, aes(x = ped.length, y = value, color = variable)) + 
    geom_point(size=2) + 
    theme_bw() + 
    facet_wrap(~spp, scales = "free_y") 

.

2 개의 변수가 있든 20 개이든 상관없이 geom_point() 하나만 있으면되기 때문에 Drew의 (완전히 유효한) 접근 방식을 선호합니다. 다양한 미학을 쉽게 매핑 할 수 있습니다. .