2014-06-19 3 views
1

데이터 프레임의 일부를 기반으로 별도의 선을 그리려하고 있지만 연결하면 연결하지 않을 점에 선이 그려집니다. geom_segment를 사용geom_line을 사용하여 요인의 하위 집합에 대한 선을 그립니다.

수동으로 그리는 나는 (마이너스 올바른 전설) 이후 해요 무엇을 얻을 :

enter image description here

하지만 모두 내가 다른 그래프에 대한 내 코드를 사용할 수 있도록해야하는, geom_line를 사용할 때

enter image description here

내 데이터 프레임 (DF) :

0,123,516 또한 올바른 전설 (I 희망)을 추가, 내가 할 수있는 최선은 이것이다
df <- read.table(text='Treatment   Function Time N Rel_abund   sd   se   ci 
1  Start  "Methanogenesis" Start 3 1.983614e-04 3.839642e-05 2.216818e-05 9.538199e-05 
2  Start "Methane oxidation" Start 3 1.245265e-04 2.275417e-05 1.313712e-05 5.652448e-05 
3  Start "Sulphate Reduction" Start 3 3.693332e-05 1.247878e-05 7.204626e-06 3.099900e-05 
4 "1 x Flood"  "Methanogenesis" End 3 1.673369e-04 1.043482e-05 6.024546e-06 2.592153e-05 
5 "1 x Flood" "Methane oxidation" End 3 1.269306e-04 2.938948e-05 1.696803e-05 7.300753e-05 
6 "1 x Flood" "Sulphate Reduction" End 3 3.742168e-05 2.187629e-06 1.263028e-06 5.434372e-06 
7 "3 x Floods"  "Methanogenesis" End 3 2.135845e-04 3.762486e-05 2.172272e-05 9.346534e-05 
8 "3 x Floods" "Methane oxidation" End 3 9.097189e-05 1.192464e-05 6.884691e-06 2.962244e-05 
9 "3 x Floods" "Sulphate Reduction" End 3 8.513220e-05 2.271764e-05 1.311603e-05 5.643374e-05') 

그리고 내 코드 : 그것은 각 기능의 모든 항목을 연결 중인지에

ggplot(df, aes(x=Time, y=Rel_abund,col=Function))+geom_point(size=2,position=position_dodge(.1))+ 
    geom_errorbar(aes(ymin=Rel_abund-se,ymax=Rel_abund+se),width=0.075,position=position_dodge(.1))+ 
    geom_line(aes(group=Function),position=position_dodge(.1))+ 

나는 그것이 무엇을하고 있는지 볼 수 있지만 해당 행에 NA 두 개의 새 열을 만들 때, 그것은 여전히 ​​연결 모든 NA 사건.

궁극적으로, 다른 치료 (1 x 홍수, 3 x 홍수)의 차이 라인 유형을 포함하는 전설과 함께 내 톱 클래스와 비슷한 그래프를 원합니다. 다른 데이터 세트에 쉽게 적용되거나 수정됩니다.

미리 감사드립니다.

+0

'geom_segment (...) '를 사용하여 첫 번째 그래프를 "수동으로 그리는"코드를 보여줄 수 있습니까? 나는 당신이 어떻게 세그먼트의 시작과 끝 모두를 피할 수 있었는지 궁금하다. – jlhoward

+0

@jlhoward geom_segment (data = df, aes (x = 0.96, y = df $ Rel_abund [1], xend = 1.96, yend = df $ Rel_abund [4]), color = "# F8766D", size = .25 , linetype = 3) + 그건 내 명령 중 하나입니다. 그 중 하나는 각 행에 하나씩 있습니다. 라인의 시작과 끝의 옵셋에 대해 질문하는 경우, x 좌표를 지정하고 적절한 경우 플러스 또는 마이너스 0.04만큼 오프셋합니다 (예를 들어 0.04 라인을 왼쪽으로 이동). 내가 말했듯이, 그것은 매우 수동적 인 접근이었습니다! 도움이 되었으면 좋겠어. 원한다면 모든 것을 보여줄 수있어. – RB88

답변

1

불행히도 어쨌든 선에 대한 데이터를 재구성하는 것 이외에는이 작업을 수행 할 수 없습니다. 여기에 내가

Time Rel_abund   Function Treatment     id 
1 Start 1.245265e-04 Methane oxidation 1 x Flood Methane oxidation 1 
2 Start 1.245265e-04 Methane oxidation 3 x Floods Methane oxidation 2 
3 End 1.269306e-04 Methane oxidation 1 x Flood Methane oxidation 1 
4 End 9.097189e-05 Methane oxidation 3 x Floods Methane oxidation 2 
5 Start 1.983614e-04  Methanogenesis 1 x Flood  Methanogenesis 1 
6 Start 1.983614e-04  Methanogenesis 3 x Floods  Methanogenesis 2 
7 End 1.673369e-04  Methanogenesis 1 x Flood  Methanogenesis 1 
8 End 2.135845e-04  Methanogenesis 3 x Floods  Methanogenesis 2 
9 Start 3.693332e-05 Sulphate Reduction 1 x Flood Sulphate Reduction 1 
10 Start 3.693332e-05 Sulphate Reduction 3 x Floods Sulphate Reduction 2 
11 End 3.742168e-05 Sulphate Reduction 1 x Flood Sulphate Reduction 1 
12 End 8.513220e-05 Sulphate Reduction 3 x Floods Sulphate Reduction 2 

그래서 우리는 각 기능/치료 조합에 대한 시작 값을 복제 한 생산할 예정이다
ld<-do.call(rbind, lapply(split(df, df$Function), function(x) { 
    s <- x$Time=="Start"; 
    ids <- paste(x$Function[!s], 1:sum(!s)) 
    cols <- c("Time","Rel_abund", "Function") 
    suppressWarnings(rbind(
     cbind(x[s, cols], Treatment=x$Treatment[!s], id=ids), 
     cbind(x[!s, c(cols, "Treatment")], id=ids) 
    )) 
})) 

할 것 변환합니다. 그런 다음 양쪽 끝 값을 유지했습니다. 시작 값에 대한 트리 트먼트를 업데이트하여이를 기반으로 스타일을 지정할 수도 있습니다. 마지막으로 qqplot이 연결할 시작/끝 지점을 알 수 있도록 ID를 추가했습니다. 그것은 가장 예쁜 변환이 아닐지도 모르지만 그것은 일을 완성시킵니다.

이제 우리는 우리가 처음으로 새로운 명령에 대해 원래 data.frame을 사용

library(ggplot2) 
ggplot(df, aes(x=Time, y=Rel_abund, col=Function))+ 
    geom_point(size=2,position=position_dodge(.1))+ 
    geom_errorbar(aes(ymin=Rel_abund-se, ymax=Rel_abund+se), 
     width=0.075, position=position_dodge(.1))+ 
    geom_line(data=ld, aes(group=id, lty=Treatment), 
     position=position_dodge(.1) 
) 

주의와 플롯을 그리고 단지 geom_line()에 대한 우리의 특별한 데이터 집합을 지정할 수 있습니다. 그리고 그것은 우리에게 첫 번째 사진에 꽤 가까운

enter image description here

을 줄 것이다.

0

여기는 geom_segment(...)을 사용하고 데이터 세트를 재구성 할 필요가없는 접근 방법입니다. 그러나 해킹 비트라는 것을 인정해야합니다.그래서 여기

df$Start <- merge(df,df[df$Time=="Start",c(3,5)],by="Time")$Rel_abund.y 
df$Time <- factor(df$Time,levels=c("Start","End")) 

library(ggplot2) 
ggplot(df, aes(x=Time, y=Rel_abund,col=Function))+ 
    geom_point(size=2,position=position_dodge(.1))+ 
    geom_errorbar(aes(ymin=Rel_abund-se,ymax=Rel_abund+se),width=0.075,position=position_dodge(.1))+ 
    geom_segment(data=df[df$Time!="Start",], 
       aes(x=1, xend=2+(as.numeric(Function)-2)*0.04, y=Start, yend=Rel_abund, color=Function , linetype=Treatment), 
       position=position_dodge(0.1)) 

우리는 Rel_abund의 시작 값을 포함 df에 여분의 열, Start을 추가, - 즉 우리가 Function의 각 값에 대해 df[df$Time=="Start",]$Rel_abund을 복제합니다. merge(...)을 사용하면 값이 Function의 다른 값에 대해 고유하게 지정됩니다. 거기에서 한 가지를 제외하면 geom_segment(...)을 사용하는 것이 간단합니다. x 값을 다듬 으려합니다.

geom_segment(...)을 피하는 문제는 이 아닌 x에만 적용됩니다. 위의 코드는 0.04*(as.numeric(Function)-2)xend에 추가하여 해킹합니다.

관련 문제