1

Graph needing to be recreated:

enter image description here

Here is my dataset:

enter image description here

I have a very similar dataset, but my data has internal temperature of Yellowfin tuna and external temperature of surrounding water.

I am struggling to also recreate the secondary Y axis on the right side of the graph, as i do not have a bespoke temperature column, so creating a temperature column and directly linking them to the data labels of internal and external temp.

As this graph is quite specialized, i cannot find information to create it.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Nathan24
  • 9
  • 2
  • Please add the link to the example plot, share reproducible example data `dput(myData)`. – zx8754 Jan 21 '22 at 11:51
  • I have now updated. i have only put a sceenshot of my data, as i am unsure of how to link entire datasets – Nathan24 Jan 21 '22 at 12:05
  • 1
    How to add example data, see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – zx8754 Jan 21 '22 at 13:22

1 Answers1

0

Using ggplot2

Here is solution using an example data.frame:

# Build dummy data
data <- data.frame(
  time = seq(c(ISOdate(2000,3,20)), by = "min", length.out = 100),
  temperature = runif(100, 0,30), 
  temperature2 = runif(100,0,30), 
  depth = runif(100,0,1200) 
)

ggplot(data) +
  geom_line(aes(x=time, y=depth, color="Depth"), size=1) +
  geom_line(aes(x=time, y=temperature*40-30, color="Perit. Temp"), size=1) +
  geom_line(aes(x=time, y=temperature2*40-30, color="Ambien Temp."), size=1) +
  scale_x_datetime(position="top") +
  scale_y_continuous(
    name = "Depth (m)",
    trans="reverse",
    sec.axis = sec_axis(trans= ~.*-1/40+30, name="Temperature (C)")) +
  labs(x = "Time (local)") +
  theme_bw() +
  scale_colour_manual(name="Legend",values=c("Depth"="black", "Ambien Temp."="blue", "Perit. Temp"="red") ) +
    theme(legend.position = c(0.87, 0.25))

If you want to use a secondary y-axis, you have to use a transformation from the original y-axis (the point in the formula indicates the value of original y-axis) and apply the reverse transformation on your data which you want to use for the secondary y-axis.

If you want to use the x-axis on the top you have to take care to use the correct function, e.g. here it is scale_x_datetime because we ware using a datetime.

Also note, that the names to create your legend (scale_colour_manual) must be exactly the same as in your aes-argument.

enter image description here

JKupzig
  • 1,226
  • 3
  • 13