2016-05-17 60 views
1

我想添加一個自定義的圖例,在我的圖中標出水平線。我需要的傳說說明如下,也需要被定位出被繪製藍色線條的方式:如何在R中使用ggplot2爲水平線創建自定義圖例

驗收標準:

紅線:「系統族= 1」

橙行: 「錨隆起」

鋼青行: 「系統族= 2」

綠色行: 「系統族= 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包含我們想要擁有圖例的條件行的數據。請注意,數據是「長」格式。例如,在datlevel列中告訴我們哪個分組xy值屬於。

  2. geom_line繪製數據。通過設置group=level,我們爲level的每個值分別獲得一行,只需撥打geom_line。通常情況下,我們還會在aes的內部添加color=level以獲得每行不同的顏色。但是,我們只需要一個標準線的圖例,所以我們需要「手動」將顏色映射到數據線。這就是colour=rep(hcl(210,100,seq(20,90,length.out=8)), each=100)所做的(請注意,它不在aes之外)。

  3. geom_text將值標籤放置在每條曲線的右端。請注意,我們對數據進行過濾,以便我們只保留每個level的最右側(x,y)值,並且我們用它將文本放在每行的旁邊。

  4. geom_abline繪製出Criteria線。我們在aes內部使用color=Criteria,以便Criteria線條以不同的顏色繪製,並且我們獲得了顏色圖例。

這裏的圖是什麼樣子:

enter image description here

UPDATE:這裏與您發佈的數據的另一個例子。我將你的數據文件加載到一個名爲df的數據框中。它看起來像您對數據進行了子集化處理,我認爲以下子集與您問題中的子集相同。我們希望,你就可以概括如下取其子集(S)的例子中要包含:

# 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?當我嘗試使用上面建議的方法時,我沒有得到相同的圖表。提前致謝! – Brian

+0

查看更新的答案。 – eipi10

+0

感謝您更新您的答案。當我使用filter命令時,出現以下錯誤消息:「Match.arg(method):Error'找不到對象'Water.Depth'。我已經加載plyr,我不知道是什麼原因導致了這個錯誤。 – Brian