Nathan GRAVIER
Nathan GRAVIER

Reputation: 21

Draw an isocline in R with ggplot2

I am actually trying to reproduce the background (blue isotherm, isohalin) of this graph:

graph with an isocline

With that kind of file (csv): https://drive.google.com/file/d/1VMjDOFP2ZlVTsNuEVmMwQAxFdLAahdSk/view?usp=sharing

The idea is to plot Depth (y) on Distance (x) from each stations to another. I would like to draw isoclines on it with the "Temp" column.

I tried with ggplot2, and with the geom_area, geom_density2d, or geom_bind2d functions, but I can't succeed making it working. (in this script for example, the error it is showing is Error in f(...) : Aesthetics can not vary with a ribbon

I feel stupid because this is the first time I am using ggplot2.
Could you help me please ?

library(readr)
Tr3 <- read_delim("G:/Tr3.csv", ";", escape_double = FALSE, 
                  col_types = cols(Depth = col_number(), 
                                   Distance = col_number(), Temp = col_number()), 
                  trim_ws = TRUE)
library(ggplot2)
theme_set(
  theme_bw() + 
    theme(legend.position = "top"))
f<-ggplot(Tr3, aes(x=Distance, y=Depth))+
  geom_area(aes(x=Distance, y=Depth, fill=Temp))
f  

Thank you for everything !

Upvotes: 2

Views: 552

Answers (1)

Calum You
Calum You

Reputation: 15072

geom_contour_filled gives essentially what you're looking for, I think:

library(tidyverse)
tr3 <- read_delim("Downloads/Tr3.csv", delim = ";")
ggplot(tr3) +
  geom_contour_filled(aes(x = Distance, y = Depth, z = Temp))

but the data you provided does not seem super conducive to the kind of plot you showed, with only 6 unique distances (points at which a probe was dropped?) and very similar temperatures at many depths. I added a point geom with about the same colour scale to make it clearer where the interpolation is happening in the image below.

ggplot(tr3, aes(x = Distance, y = Depth)) +
  geom_contour_filled(aes(z = Temp), binwidth = 0.5) +
  geom_point(aes(colour = Temp)) +
  scale_colour_viridis_b(breaks = seq(5.5, 11, by = 0.5)) +
  coord_cartesian(ylim = c(0, 60))

enter image description here

Upvotes: 3

Related Questions