2016-04-06 11 views
1

私は線の間の領域が変数の係数に基づいて塗りつぶされたプロットを作成したいと思います。私は、線ya > ybと緑の場合は、多角形を、緑色の場合はyb > yaとします。 geom_polygonでの色の変化

xa <- c(1,2,3,4,5,6) 
ya <- c(1,10,15,7,12,3) 
xb <- c(1,2,3,4,5,6) 
yb <- c(5,10,14,12,2,11) 
toyset <- as.data.frame(cbind(xa, ya, xb, yb)) 
toyset$color<-ifelse(toyset$yb>ya,"green","red") 

poly_df <- rbind(setNames(toyset[,c(1,2,5)],c('x','y','color')), 
      setNames(toyset[6:1,3:5],c('x','y','color'))) 

この

はまったく機能していません。

ここ
ggplot(toyset) + 
geom_line(aes(xa, ya), colour="red") + 
geom_line(aes(xb, yb), colour="green") + 
geom_polygon(data = poly_df,aes(x = x,y = y,fill=poly_df$color)) 

ポリゴンは大丈夫ですが、全く異なる色が存在しない(画像を参照してください):

ggplot(toyset) + 
geom_line(aes(xa, ya), colour="red") + 
geom_line(aes(xb, yb), colour="green") + 
geom_polygon(data = poly_df,aes(x = x,y = y),fill=poly_df$color) 

plot with no colour variation

+2

私はあなたが、あなたが 'geom_polygon(データ= poly_df、AES(Xを使用することができ、最初の5つの多角形のそれぞれの座標を定義し、各ポリゴンにIDと色を割り当てる必要が推測= x、y = y、塗りつぶし= id、色= id)) ' – beetroot

+0

私はすでにpoly_dfを作成していませんか? – PvGelder

+0

私はそうは思わない。また、両方の線が交差する点を含める必要があります。 – beetroot

答えて

1

このblogの例に従って、次のように進めます。

我々は、2本の線が交差する各点で塗りつぶし色を変更するために

x1 <- c(1, 2, 3, 4, 5, 6) 
y1 <- c(1, 10, 15, 7, 12, 3) 
y2 <- c(5, 10, 14, 12, 2, 11) 
toyset <- as.data.frame(cbind(x1, y1, y2)) 

(微調整)初期玩具データセットを使用して、我々は、交点を計算します。

toyset$slope1 <- c(NA, with(toyset, diff(y1)/diff(x1))) 
toyset$slope2 <- c(NA, with(toyset, diff(y2)/diff(x1))) 
toyset$intcpt1 <- with(toyset, y1 - slope1 * x1) 
toyset$intcpt2 <- with(toyset, y2 - slope2 * x1) 
toyset$x2 <- with(toyset, (intcpt1 - intcpt2)/(slope2 - slope1)) 
toyset$y3 <- with(toyset, slope1 * x2 + intcpt1) 
toyset <- toyset[, c(-4:-7)] 

我々は、視覚的に交点の位置を確認することができ、計算が正しいことを確認するには、次の

ggplot(toyset) + geom_line(aes(x1, y1), colour = "red") + 
    geom_line(aes(x1, y2), colour = "darkgreen") + 
    geom_point(aes(x2, y3), colour = "darkblue", size = 3) 

我々はgeom_ribbonを使用するように、交差点のポイントを提示することも必要geom_ribbon(x, ymin, ymax)によって予期される形式で - y3の単純なコピーがこれを実行します。

toyset$y4 <- toyset$y3 

追加のエラーチェックを行い、各データポイントに適切な間隔を割り当てます。 ggplot2について

toyset[which(toyset$x2 > toyset$x1), c("x2", "y3", "y4")] <- NA 
toyset$segment <- findInterval(toyset$x1, 
           c(min(toyset$x2, na.rm = TRUE), 
            toyset$x2[which(!is.na(toyset$x2))])) 

線の各交差点で塗りつぶしの色を変化させることができるためには、各着色領域の始点と終点を知る必要があります。つまり、交差点の中点は、異なる色で塗りつぶされた2つの隣接するエリアの一部であるため、複製する必要があります。

toyset$x3 <- c(tail(toyset$x2, -1), NA) 
toyset$y5 <- c(tail(toyset$y3, -1), NA) 
toyset$y6 <- toyset$y5 

ここで、2つのラインの座標とカラー領域の開始点と終了点を、長いフォーマットの1つのdata.frameに結合する必要があります。

toyset1 <- toyset[, c(1:3, 7)] 
toyset2 <- toyset[!is.na(toyset$x2), c(4:6, 7)] 
toyset3 <- toyset[!is.na(toyset$x3), c(8:10, 7)] 

names(toyset2) <- names(toyset1) 
names(toyset3) <- names(toyset1) 

combo <- rbind(toyset1, toyset2) 
combo <- rbind(combo, toyset3) 
combo <- combo[is.finite(combo$y1), ] 
combo <- combo[order(combo$x1), ] 

これでセグメントを適切に入力できるようになりました。

ggplot(combo, aes(x1, ymin = y1, ymax = y2))+ 
    geom_ribbon(aes(fill = factor(segment%%2)), alpha = 0.5) + 
    geom_path(aes(y = y1), colour = "red", size = 1) + 
    geom_path(aes(y = y2), colour = "darkgreen", size = 1) + 
    scale_fill_manual(values = c("red", "green")) 

Segments filled

+1

ああ、これは動作するようです!ありがとう!ここで完全な回答を投稿しても、回答としてマークすることができます。ブログをリンクするだけで、時間の経過とともに情報が失われる可能性があります。 – PvGelder

+0

申し訳ありませんが、私はかなり新しいstackoverflowです。この問題に対するより簡単なアプローチがあるかどうかはわかりませんが、私の全回答が役立つことを願っています! – rhole

関連する問題