Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plotting object with season dimension = 12 #13

Open
marchtaylor opened this issue Sep 13, 2018 · 18 comments
Open

plotting object with season dimension = 12 #13

marchtaylor opened this issue Sep 13, 2018 · 18 comments
Assignees

Comments

@marchtaylor
Copy link

marchtaylor commented Sep 13, 2018

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:

ggplotfl

adjusted version with ggplot2:

    DIMNAMES <- dimnames([email protected])
    df <- as.data.frame([email protected])
    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
Copy link
Member

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
Copy link
Author

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

@iagomosqueira
Copy link
Member

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
Copy link
Author

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

@iagomosqueira
Copy link
Member

iagomosqueira commented Mar 4, 2019

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

@iagomosqueira iagomosqueira reopened this Mar 4, 2019
@marchtaylor
Copy link
Author

marchtaylor commented Mar 4, 2019

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
Copy link
Author

marchtaylor commented Mar 4, 2019

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
Copy link
Member

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

iagomosqueira added a commit that referenced this issue Mar 4, 2019
@iagomosqueira
Copy link
Member

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

@marchtaylor
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Author

here is an example of the behavior:

data("ple4")
stkn <- [email protected]
stkn[1,] <- NaN
stkn[,ac(1981:2017)] <- NaN
plot(stkn)

rplot

@marchtaylor
Copy link
Author

marchtaylor commented Mar 4, 2019 via email

@iagomosqueira
Copy link
Member

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants