2016-05-17 6 views
1

私のプロットの水平線をラベル付けしたカスタム凡例を追加しようとしています。私は、次のように述べること伝説を必要とし、また、プロットされている青い線の邪魔にならないように配置する必要があります水平線にggplot2を使ってRでカスタム凡例を作成する方法

合格基準:

レッドライン:「FOS = 1」

オレンジライン: "アンカー浮き上がり"

Steelblueライン: "FOS = 2"

緑線: "FOS = 2.5"

Plot without legend

ここは私のRコードです。本当にありがとう!!

## Plot all Hs values 
colfunc <- colorRampPalette(c("cyan", "blue")) 
plotColors <- colfunc(length(unique(data$Wave.Height))) 
# Initialize plot 
plot1 <- ggplot(data = hedron_1kt_180deg_1ft, aes(Wind.Speed, Total.Force)) + 
    geom_line(color = plotColors[1], size = 1) + xlab("Wind Speed [knots]") + ylab("Total Force [MT]") + ggtitle("Current Speed 1 knot/Head Seas") + 
    scale_x_continuous(breaks = c(10,20,30,40,50,60,70)) + coord_cartesian(xlim = c(10,75)) + theme_bw(base_size = 20) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_1ft$Total.Force), label = "1 ft", size = 6) 
# Add all other lines 
plot1 <- plot1 + geom_line(data = hedron_1kt_180deg_1.75ft, aes(Wind.Speed, Total.Force), color = plotColors[2], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_1.75ft$Total.Force), label = "1.75 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_2.5ft, aes(Wind.Speed, Total.Force), color = plotColors[3], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_2.5ft$Total.Force), label = "2.5 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_3.25ft, aes(Wind.Speed, Total.Force), color = plotColors[4], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_3.25ft$Total.Force), label = "3.25 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_4ft, aes(Wind.Speed, Total.Force), color = plotColors[5], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_4ft$Total.Force), label = "4 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_4.75ft, aes(Wind.Speed, Total.Force), color = plotColors[6], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_4.75ft$Total.Force), label = "4.75 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_5.5ft, aes(Wind.Speed, Total.Force), color = plotColors[7], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_5.5ft$Total.Force), label = "5.5 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_6.25ft, aes(Wind.Speed, Total.Force), color = plotColors[8], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_6.25ft$Total.Force), label = "6.25 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_7ft, aes(Wind.Speed, Total.Force), color = plotColors[9], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_7ft$Total.Force), label = "7 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_7.75ft, aes(Wind.Speed, Total.Force), color = plotColors[10], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_7.75ft$Total.Force), label = "7.75 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_8.5ft, aes(Wind.Speed, Total.Force), color = plotColors[11], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_8.5ft$Total.Force), label = "8.5 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_9.25ft, aes(Wind.Speed, Total.Force), color = plotColors[12], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_9.25ft$Total.Force), label = "9.25 ft", size = 6) + 
    geom_line(data = hedron_1kt_180deg_10ft, aes(Wind.Speed, Total.Force), color = plotColors[13], size = 1) + 
    annotate("text", x = 73.5, y = max(hedron_1kt_180deg_10ft$Total.Force), label = "10 ft", size = 6) + 
    geom_hline(yintercept = 506, color = "red", size = 1) + geom_hline(yintercept = 202, color = "lawngreen", size = 1) + 
    geom_hline(yintercept = 253, color = "steelblue", size = 1) + geom_hline(yintercept = 432, color = "orange", size = 1) 
# Save plot 
png(filename = "Hedron-WD=40ft-Heading=180deg-Uc=1knot.png", width = 800, height = 600) 
plot1 
dev.off() 

ここにサンプルデータへのリンクがあります。あなただけのあなたが「長い」形式でデータフレームを入れ、その後に審美的なマッピングを使用してはるかに少ない労力で、このようなプロットを作成することができます

https://www.dropbox.com/s/htfm3h9s2rcajd4/Hedron%20and%20Arapaho%20Forces.csv?dl=0

+1

あなたのプロットを作成するために使用されるデータのサンプルを提供する場合あなたを助けるためにはるかに容易になります。あなたの質問に 'dput(my.data.sample)'の出力を貼り付けてください。 – eipi10

+0

私はDropboxリンクを介して上記のプロットを作成するために使用されたデータへのリンクを追加しました。 – Brian

答えて

1

に動作するようにコードを作業ディレクトリを変更する必要があります色と伝説を手に入れよう。

あなたはので、ここで偽のデータとの一例ですが、サンプルデータを提供していない:

## Create some fake data 

# Fake data 
dat = do.call(rbind, 
       lapply(seq(1,10, length.out=8), function(i) { 
    data.frame(level=i, x=1:100, y= 1:100 + 10*i) 
})) 

# Fake criteria lines 
dat2 = data.frame(yint = c(40, 60, 149, 180), 
        slope=c(-0.01,-0.05,-0.1, -0.08), Criteria=LETTERS[1:4]) 

library(dplyr) 

ggplot() + 
    geom_line(data=dat, aes(x,y, group=level), 
      colour=rep(hcl(210,100,seq(20,90,length.out=8)), each=100)) + 
    geom_text(data=dat %>% group_by(level) %>% filter(x==max(x)), 
      aes(label=round(level,2), x=x+1, y=y), hjust=0) + 
    geom_abline(data=dat2, aes(intercept=yint, slope=slope, color=Criteria)) + 
    theme_bw() + 
    scale_color_manual(values=c("red","orange","green","purple")) 

ここで、上記のコードで何が起こっているのだ。

  1. datは、私たちがプロットしたいデータが含まれています。 dat2には、凡例を作成する基準線のデータが含まれています。データは「ロング」形式であることに注意してください。たとえば、datでは、levelの列は、xyの値がどのグループに属するかを示します。

  2. geom_lineはデータをプロットします。 group=levelを設定すると、levelという値ごとに別々の行が得られ、geom_lineを1回呼び出すだけです。通常は、color=levelaesに追加して、各行に異なる色を付けます。しかし、我々は唯一の基準線の凡例が欲しいので、我々は "手で"データ線に色をマップする必要があります。それはcolour=rep(hcl(210,100,seq(20,90,length.out=8)), each=100)aesの外にあることに注意してください)です。

  3. geom_text各曲線の右端に値ラベルを配置します。データのフィルタリングでは、各levelの右端(x、y)の値のみを保持し、それを使って各行のすぐ隣にテキストを配置します。

  4. geom_ablineは、Criteriaラインをプロットします。我々はcolor=Criteriaの中にaesを使用して、Criteriaの線を異なる色でプロットし、色の凡例を得る。ここでは、ポストされたデータを持つ別の例です:

    enter image description here

    UPDATE:

は、ここのようなグラフが見えるものです。あなたのデータファイルをdfというデータフレームにロードしました。あなたのデータをサブセット化したようですが、私は下のサブセットがあなたの質問にあるものと同じだと思います。

# Subset data 
df.sub = df %>% filter(Barge.Name=="Hedron", 
         Heading==180, 
         Current.Speed==1, 
         Water.Depth==40) 

ggplot(data = df.sub, 
     aes(Wind.Speed, Total.Force, group=Wave.Height, color=Wave.Height)) + 
    geom_line(size = 1) + 
    geom_text(data=df.sub %>% filter(Wind.Speed==max(Wind.Speed)), 
      aes(label=Wave.Height, y=Total.Force, x=Wind.Speed + 0.5), hjust=0) + 
    theme_bw() + 
    guides(color=FALSE) 

enter image description here

あなたはまた、可視化でより多くの変数が含まれるようにファセットを使用することができます:

うまくいけば、あなたが含めたい方サブセット(複数可)に、以下の例を一般化することができるでしょう
df.sub = df %>% filter(Current.Speed==1, 
         Water.Depth==40) 

ggplot(data = df.sub, 
     aes(Wind.Speed, Total.Force, group=Wave.Height, color=Wave.Height)) + 
    geom_line(size = 1) + 
    geom_text(data=df.sub %>% filter(Wind.Speed==max(Wind.Speed)), 
      aes(label=Wave.Height, y=Total.Force, x=Wind.Speed + 0.5), hjust=0) + 
    theme_bw() + 
    guides(color=FALSE) + 
    facet_grid(Barge.Name ~ Heading) + 
    scale_x_continuous(limits=c(10,78)) 

enter image description here

+0

おかげで私の質問に答えました!あなたの指示に従ってカスタムレジェンドをプロットすることができました。私のコードで提供したサンプルデータを見ると、コードの行数をgeom_lineへの1回の呼び出しにどのように減らすことができますか?上記の方法を試しても、同じグラフが得られません。前もって感謝します! – Brian

+0

更新された回答を参照してください。 – eipi10

+0

あなたの答えを更新していただきありがとうございます。フィルタコマンドを使用すると、次のエラーメッセージが表示されます。 "match.arg(メソッド)のエラー:オブジェクト 'Water.Depth'が見つかりません"。私はplyrをロードして、このエラーの原因を突き止めていません。 – Brian

関連する問題