0

I would like to add a non-linear model with confidence band to my scatterplot. For the explanation, I am using vertical depth profile data of temperature, salinity etc. from 0 to 1000 m. After swapping the axes around, I unfortunately failed to add a regression model with confidence band. I would appreicate if any of you could help me getting started with this.

My data look like this:

     CTD Area Station Cast    Lat  Long Month Day Hour Temp  Sal    DO Fluo Depth
898 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.9  1.7   1.0
899 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.4  1.5   2.0
900 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.7  1.5   3.0
901 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.5  1.6   4.0
902 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.3  1.8   5.0
903 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.4  1.9   6.0
904 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.8  1.8   7.0
905 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.7  1.7   7.9
906 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.2  1.7   8.9
907 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.2  2.1   9.9
908 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.1  1.9  10.9
909 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.2  1.7  11.9
910 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.4  2.3  12.9
911 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.8  1.7  13.9
912 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.9  1.4  14.9
913 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 283.0  1.5  15.9
914 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.9  1.4  16.9
915 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 282.4  1.6  17.9
916 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 281.5  1.6  18.9
917 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 280.9  1.5  19.9
918 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 281.9  1.9  20.9
919 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 284.8  1.4  21.9
920 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 286.4  1.5  22.8
921 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 286.5  1.8  23.8
922 9102    1      12    1 -44.59 174.2    10  24   20 10.7 34.6 285.7  1.8  24.8

In the given example the temperature in the uppermost 25 m isn't changing but believe me, it does below.

So far my code looks like this:

p1<-ggplot(A1,aes(x=Temp,y=Depth,group=Cast))+
  geom_point()+xlim(c(6,12))+scale_y_reverse(lim=c(500,0))
p1
OceanSun3
  • 31
  • 4
  • 8
    What is your model? Most models will give you predictions and confidence bands, you can add those to your plot. – Kent Johnson Jan 13 '20 at 01:50
  • I don't have a model yet; not sure how to come up with one best. The data look like a polynomal shape, def not linear or exponental. – OceanSun3 Jan 13 '20 at 01:55
  • 2
    If you don't have a model you can fit a non-parametric function on your `y` and `x` variables of interest (which I presume are the temperature and depth, respecitvely). For this you could use the `lowess` or `smooth.spline` functions in the `stats` package or the `gam` function in the `mgcv` function. The documentation for the latter reads: "Confidence/credible intervals are readily available for any quantity predicted using a fitted model." The answers to the following question may also come handy: https://stackoverflow.com/questions/23852505/how-to-get-confidence-interval-for-smooth-spline – mastropi Jan 13 '20 at 08:00
  • Thank you, your advice is greatly appreciated! – OceanSun3 Jan 25 '20 at 00:52

1 Answers1

0

ggplotprovides the possibility to automatically fit a LOESS curve with geom_smooth:

ggplot(A1,aes(x=Temp,y=Depth,group=Cast)) +
  geom_point() +
  geom_smooth() +
  xlim(c(6,12)) + 
  scale_y_reverse(lim=c(500,0))

However, it's a question, if that fits your need if you don't have the actually model you would like to show.

Georgery
  • 7,643
  • 1
  • 19
  • 52