如何使用ggplot2为水平线创建R中的自定义图例

时间:2016-05-17 00:18:27

标签: r ggplot2

我正在尝试添加一个标注水印线的自定义图例。我需要使用图例说明以下内容,并且还需要将其放置在绘制的蓝线之外:

接受标准:

红线:“FoS = 1”

橙色线:“锚定隆起”

Steelblue line:“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 个答案:

答案 0 :(得分:1)

您可以通过将数据框放在&#34; long&#34;中来创建这样的图,而不需要太多努力。格式,然后使用美学映射来获取颜色和图例。

您尚未提供样本数据,因此以下是假数据的示例:

## 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包含我们希望拥有图例的条件行的数据。请注意,数据在&#34; long&#34;格式。例如,在dat level列中告诉我们xy值属于哪个分组。

  2. geom_line绘制数据。通过设置group=level,只需拨打一次level即可为geom_line的每个值获取单独的行。通常情况下,我们还会在color=level内添加aes以获得每行的不同颜色。但是,我们只需要标准线的图例,因此我们需要将颜色映射到数据线&#34;手动&#34;。这是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行以不同的颜色绘制,我们会得到一个颜色图例。

  5. 这是图表的样子:

    enter image description here

    更新:这是您发布的数据的另一个示例。我将您的数据文件加载到名为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