如何更改和/或修改两个变量的构面标签?

时间:2016-05-18 16:49:03

标签: r ggplot2

我正在尝试在下图中更改“标题”和“当前速度”的构面标签。我想要的最终结果是将“标题:90”标签说成“标题:90度”。标题“当前速度:1”表示“当前速度:1节”等。

我在想,使用Heading和Current.Speed的值将字符串粘贴在一起会更简单,但我似乎无法弄明白。

任何帮助将不胜感激!

以下是我正在使用的数据的链接:https://www.dropbox.com/s/htfm3h9s2rcajd4/Hedron%20and%20Arapaho%20Forces.csv?dl=0

Example Plot

library(ggplot2)
library(magrittr)
library(dplyr)


## Set the working directory and read the data
df <- read.csv("Hedron and Arapaho Forces.csv", header = TRUE, sep = ",")

    ### Hedron Environmental Forces (Water Depth = 40ft) ###
## Subset the data
df.sub1 = df %>% filter(Barge.Name=="Hedron",
                        Water.Depth==40)

g1 <- ggplot(data = df.sub1,
       aes(Wind.Speed, Total.Force, group=Wave.Height, color=Wave.Height)) + 
  geom_line(size = 0.5) + 
  geom_text(data = df.sub1 %>% filter(Wind.Speed==max(Wind.Speed)),
            aes(label=paste("Hs = ", Wave.Height, " ft"), y=Total.Force, x=Wind.Speed + 0.5), hjust=0, size=2) +
  theme_bw() + guides(color=FALSE) + facet_grid(Current.Speed ~ Heading, labeller = label_both) + 
  scale_x_continuous(limits = c(10,78), breaks = c(10,20,30,40,50,60,70)) +
  ggtitle("Tetra Hedron Environmental Forces (Water Depth=40ft)") + xlab("Wind Speed [knots]") + 
  ylab("Total Force [MT]") + geom_hline(yintercept = 506, color="red", size=0.5) + 
  geom_hline(yintercept = 202, color="lawngreen", size=0.5) + geom_hline(yintercept = 432, color="orange", size=0.5) +
  geom_hline(yintercept = 36, color="black", size=0.5) +
  annotate("text", x=10, y=550, label="Fos=1", size = 2, color="red") +
  annotate("text", x=11, y=475, label="Anchor Uplift", size = 2, color="orange") +
  annotate("text", x=10, y=245, label="FoS=1", size = 2, color="lawngreen") + 
  annotate("text", x=11, y=80, label="Anchor Drag", size = 2, color="black")

1 个答案:

答案 0 :(得分:1)

将Current.Speed更改为标签为数字的因子以及&#34; knots&#34;:

df.sub1$Current.Speed.f <- factor(df.sub1$Current.Speed, 
                                  levels=sort(unique(df.sub1$Current.Speed)), 
                                  labels=paste(sort(unique(df.sub1$Current.Speed)), "knots"))

在这里,我创建了一个新变量,以免破坏原始变量。