2

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 !

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87

1 Answers1

3

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

Calum You
  • 14,687
  • 4
  • 23
  • 42
  • haven't seen you on ggplot2 tag in a while :) Worthwhile to mention maybe also the metR package which has `metR::geom_contour_fill()`. The difference being it somehow keeps the continuous nature of the data and you can use the new binning scales for cooler color bars (https://stackoverflow.com/questions/62543112/how-to-make-discrete-gradient-color-bar-with-geom-contour-filled/62571158#62571158) – tjebo Jul 07 '20 at 06:39
  • 1
    things have been happening in the world as of late haha. and that's a great function! TIL – Calum You Jul 07 '20 at 09:35
  • Hello ! I tried to use your code as you wrote it, and unfortunately, it gives me an error: `Computation failed in `stat_contour_filled()`: Number of x coordinates must match number of columns in density matrix.` Your suppositions are right, we launched a probe. We got 30 points with the hydrologic datas associated, and we tried to divide them in transects as it should provide an easier interpretation of distance between stations. – Nathan GRAVIER Jul 08 '20 at 07:16
  • Can you check your R and ggplot2 versions? I'm updated to 4.0.2 and 3.3.2 respectively. stat_contour_filled was updated in ggplot 3.3. But I still wanted to clarify that something like the displayed image is what you're looking for, since i'm unclear how meaningful the contours are here. Also see the answer linked by Tjebo – Calum You Jul 08 '20 at 22:09
  • Hi Calum, My R version is 3.6.3 and ggplot2, 3.3.1. The picture I get hqs no problems with colors. the biggest problem is that column are really thin and they don't fill the whole space, resulting in having white spaces between each stations where there is data points. I will check Tjebo's answer a little bit later. Thank you, Nathan – Nathan GRAVIER Jul 10 '20 at 08:14