我想添加一個自定義的圖例,在我的圖中標出水平線。我需要的傳說說明如下,也需要被定位出被繪製藍色線條的方式:如何在R中使用ggplot2爲水平線創建自定義圖例
驗收標準:
紅線:「系統族= 1」
橙行: 「錨隆起」
鋼青行: 「系統族= 2」
綠色行: 「系統族= 2.5」
這是我的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
這將是更容易幫助你,如果你提供用於創建圖中的數據的樣本。將'dput(my.data.sample)'的輸出粘貼到你的問題中。 – eipi10
我已經通過Dropbox鏈接添加了用於創建上圖的數據的鏈接。 – Brian