Brian
Brian

Reputation: 427

How to change and/or modify facet labels for two variables?

I am trying to change the facet labels for "Heading" and "Current Speed" in the plot below. The end result I am trying for is to have the "Heading: 90" label to say "Heading: 90 degrees". Also the label "Current Speed: 1" to say "Current Speed: 1 knots", etc.

I was thinking there would be an easier way to just paste the string together using the values of Heading and Current.Speed, but I can't seem to figure it out.

Any help would be appreciated!

Here is a link to the data I am using: 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")

Upvotes: 0

Views: 363

Answers (1)

lmo
lmo

Reputation: 38520

Change Current.Speed to a factor where the labels are the numbers together with "knots":

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"))

Here I created a new variable so as not to destroy the original.

Upvotes: 1

Related Questions