2017-11-27 1 views
2

ggplot2를 사용하여 geom_ribbon을 사용하고 geom_line을 사용하여 중심선을 사용하여 주변 지역과 함께 여러 줄을 그려 봅니다. 값은 겹치지 만, 각 리본/라인 조합을 맨 아래 또는 맨 위에 조합하고 싶습니다. 이 코드를 사용하여 내가 원하는 플롯을 얻을 수있는 예에서ggplot2 z 여러 기수의 순서 (배경에서 전경까지)

library(ggplot2) 
x <- 1:10 
y <- c(1+x/100, 2-x, .1*x^2) 
data <- data.frame(x=rep(x,3), y=y, lower=y-1, upper=y+1, color=rep(c('green', 'blue', 'yellow'), each=10)) 

: 여기

가 재현 예제 나는 간단하게 때

ggplot() + 
    geom_ribbon(data=data[data$color=='green',],aes(x=x, ymin=lower, ymax=upper, fill=paste0('light',color))) + 
    geom_line(data=data[data$color=='green',],aes(x=x, y=y, col=color)) + 
    geom_ribbon(data=data[data$color=='blue',],aes(x=x, ymin=lower, ymax=upper, fill=paste0('light',color))) + 
    geom_line(data=data[data$color=='blue',],aes(x=x, y=y, col=color)) + 
    geom_ribbon(data=data[data$color=='yellow',],aes(x=x, ymin=lower, ymax=upper, fill=paste0('light',color))) + 
    geom_line(data=data[data$color=='yellow',],aes(x=x, y=y, col=color)) + 
    scale_color_identity() + 
    scale_fill_identity() 

enter image description here

을하지만, 및 이 코드는 우리에게

plot <- ggplot(data=data) + 
    geom_ribbon(aes(x=x, ymin=lower, ymax=upper, fill=paste0('light',color))) + 
    geom_line(aes(x=x, y=y, col=color)) + 
    scale_color_identity() + 
    scale_fill_identity() 
(210)

enter image description here

배경 데이터의 라인은 '최고'리본을 통해 갈, 또는 나는 geom_line 및 geom_ribbon을 전환하는 경우, 내 중간 선은 더 이상 볼 수 있습니다.

이 예제의 경우 긴 호출이 작동하지만 실제 데이터에는 더 많은 선이 있고 백그라운드에서 전경으로 선을 동적으로 전환 할 수 있기를 원합니다.

ggplot2에게 다른 기하 도형간에 전환해야하는 순서가 있음을 알릴 수있는 방법이 있습니까?

P. 이미지를 아직 게시 할 수 없습니다. 내 질문이 불분명하면 죄송합니다.

당신은 루프로 타이핑을 절약 할 수
+1

나는 리본과 라인을 결합한 새로운 기하 구조를 작성하지 않고 그게 가능하다고 생각하지 않습니다 – baptiste

답변

3

ggplot(data=data) + 
    purrr::map(.x = split(data, f = data$color), 
       .f = function(d){ 
    list(geom_ribbon(data=d,aes(x=x, ymin=lower, ymax=upper), fill=paste0('light',unique(d$color))), 
     geom_line(data=d,aes(x=x, y=y), col=unique(d$color))) 
    })