0

I am trying to represent 2 time series variables on a plot, so as to compare them ; one has 365 values over a year (rainfall values in 2016), and the other one has 28 values (values extracted from satellite images) over the same year. The problem is that I don't know how to represent both on the same plot, with the right x and y axis.

I merged the two dataframe containing my 2 variables by adding lines in NA for the variables that only had 28 values ; now I have one dataframe with both variable.

Here is my dataframe :

> head(VH_meteo_2016, 20)
         date pmm_cat bati_VH_2016
1  2016-01-01   0.000               NA
2  2016-01-02   0.254               NA
3  2016-01-03   0.000     -23.51970255
4  2016-01-04   1.270               NA
5  2016-01-05   4.572               NA
6  2016-01-06   1.016               NA
7  2016-01-07   1.270               NA
8  2016-01-08   0.000               NA
9  2016-01-09   0.000               NA
10 2016-01-10   0.000               NA
11 2016-01-11   0.000               NA
12 2016-01-12   0.000               NA
13 2016-01-13   0.000               NA
14 2016-01-14   0.000               NA
15 2016-01-15   0.000     -18.11035861
16 2016-01-16   3.556               NA
17 2016-01-17   9.652               NA
18 2016-01-18   5.080               NA
19 2016-01-19   0.000               NA
20 2016-01-20   0.000               NA

But when I try to make a plot, I only get the 365 values variable represented (I don't know exactly why), as below :

ylim.prim <- c(0, 125)
ylim.sec <- c(-25, -4)

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

ggplot(VH_meteo_2016, aes(x=date)) +
  geom_bar(aes(y=pmm_cat), stat = "identity", fill="blue") +
  geom_line(aes(y = a + VH_meteo_2016$bati_VH_2016 * b), color = "orange", size=1.25) +
  scale_y_continuous(name="Precipitations (mm)",
                     sec.axis = sec_axis(~ . * -52/125, name = "Backscatter (Sigma0 in dB)"),
                     limits = c(0,150)) +
  ggtitle("Precipitations and backscatter over 2016") +
  xlab("Time") +
  theme_light()

The result :

from https://imgur.com/l6yvXuC

Then I tried to ignore the NA values in the 28 values variable, but it also remove the corresponding values in the 365 values variable :

ylim.prim <- c(0, 125)
ylim.sec <- c(-25, -4)

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

VH_meteo_2016=na.omit(VH_meteo_2016)

ggplot(VH_meteo_2016, aes(x=date)) +
  geom_bar(aes(y=pmm_cat), stat = "identity", fill="blue") +
  geom_line(aes(y = a + VH_meteo_2016$bati_VH_2016 * b), color = "orange", size=1.25) +
  scale_y_continuous(name="Precipitations (mm)",
                     sec.axis = sec_axis(~ . * -52/125, name = "Backscatter (Sigma0 in dB)"),
                     limits = c(0,150)) +
  ggtitle("Precipitations and backscatter over 2016") +
  xlab("Time") +
  theme_light()

The result :

from https://imgur.com/AdWmiIk

Is there a way to have the first result but with the orange line from the second result (corresponding to the 28 values variable), for example by ignoring the NA but only for the 28 values variable ?

  • 1
    [Don't use `$` inside your `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio). Also, the column `pmm_cat` is in your ggplot code but not in the data you showed – camille May 02 '19 at 14:07
  • Thank you. Yes sorry, I deleted some of the columns in my dataframe for this post, but I deleted the wrong one, I modified it – J Boissonnat May 02 '19 at 14:19

0 Answers0