2012-02-18 4 views
2

I부터 그래프 작성하려는 다음의 두 데이터 세트 막대 체인 줄거리와 XY 라인 플롯 :만들고 두 플롯 결합 - R

첫번째 데이터 것은

position <- c(10, 26, 31, 50, 73, 92, 120, 124) # need scale 
    minimum 0 to maximum 130 
label <- c("A", "B", "C", "D", "E", "F", "G", "H") 
mydf <- data.frame (position, label) 

초 데이터 (저부 개발할)

:

pos <- 1:130 
value <- seq (0, 1.29, 0.01) 
mydf2 <- data.frame (pos, value) 

그래프는 (유사하거나 더 높은 품질) 개발하고자하는 (오버레이 할 선 그림을 개발할 것입니다) 0

enter image description here

내 재판

다음은 I가 완료 처음 시도 것입니다!

yvar <- rep(1, length(position)) 

require (ggplot2) 


bar <- data.frame(y = c(1, 1), x = c(0, 130)) 
ggplot() + 
    geom_line(aes(x, factor(y), group = factor(y)), 
      bar, size = 2, colour = "skyblue") + 
    geom_rect(aes(y = yvar, 
       xmin = position - 0.1, 
       xmax = position + 0.1, 
       ymin = 1 - yvar /2, 
       ymax = 1 + yvar /2)) 

답변

3

다음은 기본 그래픽이 포함 된 솔루션입니다.

# Split the plot area in two 
layout(matrix(c(1,1,2),nc=1)) 
# First plot 
plot(pos, value, type="l", las=1) 
# Reduce the margins for the second plot 
m <- par()$mar 
m[1] <- m[3] <- 0 
par(mar=m) 
# Set the limits of the second plot 
plot(pos, pos-pos, type="n", axes=FALSE, xlab="", ylab="") 
# Add the rectangle, the segments and the text. 
polygon( 
    c(0,max(mydf2$pos),max(mydf2$pos),0), 
    .2*c(-1,-1,1,1), 
    col=rgb(.1,.5,.3) 
) 
segments(mydf$position, -.5, mydf$position, .5) 
text(mydf$position, -.7, mydf$label) 
text(mydf$position, .7, mydf$position) 

enter image description here