flr / ggplotFL

The FLR - ggplot2 interface package
http://flr-project.org/ggplotFL
4 stars 3 forks source link

plotting object with season dimension = 12 #13

Open marchtaylor opened 6 years ago

marchtaylor commented 6 years ago

Hi there,

I think there may need to be an adjustment to the way the season dimension is handled in ggplotFL. In my example, I have 12 months in the season dimension. I believe season gets originally treated as a factor, and then converted to numeric. This seems to mess up the month order in my case (e.g. "1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9"). Below is a fix that re-orders the levels based on their sequence in the dimnames of the FLStock object.

direct plot with ggplotFL:

    plot(stk@stock.n)

ggplotfl

adjusted version with ggplot2:

    DIMNAMES <- dimnames(stk@stock.n)
    df <- as.data.frame(stk@stock.n)
    df$season <- factor(df$season, levels = DIMNAMES$season)
    df$date <- as.Date(paste(df$year, 
      as.numeric(df$season), "01", sep = "-"), 
      format = "%Y-%m-%d")
    p <- ggplot2::ggplot(data = df, 
      aes(x = date, y = data, group = age)) +
      facet_wrap(~ age, ncol = 1, scales = "free_y", 
        drop=TRUE, strip.position = "right") +
      scale_y_continuous(limits = c(0,NA)) +
      geom_line()
    print(p)

ggplot2

iagomosqueira commented 6 years ago

The conversion takes place in FLCore::as.data.frame(FLQuant), with date=TRUE. The date column is of class ISOdate, and season is passed as numeric. I have added a small check for seasons being months to use the first day as season start. See here

https://github.com/flr/FLCore/blob/master/R/FLQuant.R#L1037

Does this help?

marchtaylor commented 6 years ago

Works perfect. I assume that since I didn't need to add any additional arguments to plot(stk@stock.n), that the default is date=TRUE? Many thanks. Feel free to close the issue. -Marc

iagomosqueira commented 6 years ago

The call to as.data.frame inside ggplotFL::plot sets date=TRUE, yes, although only uses it in the x axis if length(season) > 1.

marchtaylor commented 5 years ago

Hi there - it looks to me as if this functionality has been lost. The season dimension is no longer correctly handled.

iagomosqueira commented 5 years ago

Have you reinstalled from github over the last few days? I have changed a bit the engine, now using our own geom_ function. Will check this is working as it should with the new code

marchtaylor commented 5 years ago

Yes, I don't know exactly when it happened, but installing one of the other packages (FLBRP?) automatically reinstalled several other dependencies as well. I tried reinstalling ggplotFL today and still get the plotting problem - Could the date conversion be the issue again (i.e. ISOdate)?

marchtaylor commented 5 years ago

It would seem to me that season needs to be changed to a character, then numeric. As is, it looks like a direct conversion to numeric. Thus, this error is possibly not seen in an object with only 4 season levels, whereas with 12 the dimension order gets jumbled.

now: as.numeric(res$season) should be: as.numeric(ac(res$season))

iagomosqueira commented 5 years ago

OK, will fix now, and will release new ggplotFL.

iagomosqueira commented 5 years ago

Could you possibly install the package in github and see if it has been fixed?

marchtaylor commented 5 years ago

Looks good now - thanks so much. One caveat: the plotting function removes empty ages / years. I think I am fine with this, but just wanted to make sure that you want this as the default.

iagomosqueira commented 5 years ago

Are you plotting a single FLQuant? It probably shouldn't, specially since the function offers you to plot dots at data=0 for NAs. Let me check

marchtaylor commented 5 years ago

Yes, a single FLQuant. The behavior you describe used to be the case, but no longer is. NAs, are converted to zeros, and dims containing all NAs are dropped completely. I can send you an example.

marchtaylor commented 5 years ago

here is an example of the behavior:

data("ple4")
stkn <- ple4@stock.n
stkn[1,] <- NaN
stkn[,ac(1981:2017)] <- NaN
plot(stkn)

rplot

marchtaylor commented 5 years ago

Thank you for this. Sorry, I didn't know it would be this complicated. Yes, line breaks (i.e. NaN during an otherwise non-NA time series) also appear to be ignored (or removed before plotting).

On Mon, Mar 4, 2019 at 3:32 PM Iago Mosqueira notifications@github.com wrote:

The old code worked as expected if the break included the start or end of the time series, so the line was shown shorter. Breaking the line is a different thing. Can be done, but requires a bit more work. Working now on getting empty panels back, and having the NA dots plotted.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/flr/ggplotFL/issues/13#issuecomment-469273126, or mute the thread https://github.com/notifications/unsubscribe-auth/AHxTEymKndPJICZLcFpdQSTg9-bhiuyAks5vTS6BgaJpZM4WmxPY .

iagomosqueira commented 5 years ago

Not at all, thanks for noticing. This works for me now

data(ple4)
x<-catch.n(ple4)
x[, ac(1965:1973)] <-NA
x[2,]<-NA
plot(x)

Can you confirm if it does for you?

It is still not working this way in the case when iters are involved, the lines get joined. So this issue can remain open for the time being

iagomosqueira commented 5 years ago

One way to achieve it might be to make geom_flquantiles to create separate geom_ribbon calls, r rathr grid_polygon ones, for each segment interrupted by NAs in the year+season dimension.

marchtaylor commented 5 years ago

Yes, thank you. The plot is working as before now. Until now, I have not been plotting objects with multiple iters. In that case, I would probably manually replace NAs with zeros if needed. Maybe there can be a separate argument in the plotting function for NA replacement? Cheers

iagomosqueira commented 5 years ago

I think changes to data are best made outside of the plot call. I will try to find a way to make the geom cut those time steps all missing.