USGS-R / gsplot

plotting foundation for timeseries reporting
Other
6 stars 14 forks source link

log axis on wrong side #414

Closed lindsayplatt closed 7 years ago

lindsayplatt commented 7 years ago

gsplot object is correct, but plotting the wrong thing

# this should only have side 4 logged, the helper functions say the right thing, but the plot shows both logged
gs <- gsplot() %>% 
    points(1:2,c(2,25)) %>% 
    points(c(1,2), c(1, 1000000000), side=4, log='y') 
gs

logged(gs, side=2)
[1] FALSE
logged(gs, side=4)
[1] TRUE

image

lindsayplatt commented 7 years ago

In print, in the side for loop we don't want to set old.par <- par(x[[side.name]]$par) . This should be done in two steps, or par() should return the old par before it was modified.

lindsayplatt commented 7 years ago

I would like to be able to log each side separately, but that pops up with warnings currently. This should log the right axis, but not the left. I think it might be related to this issue.

gs <- gsplot() %>% 
    axis(side=4) %>% 
    view(side=2, ylim=c(1,100)) %>% 
    view(side=4, ylim=c(1,100), log='y') 

Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  nonfinite axis limits [GScale(-inf,0,2, .); log=1]
2: In doTryCatch(return(expr), name, parentenv, handler) :
  nonfinite axis limits [GScale(-inf,0,2, .); log=1]

gs

Warning message:
In plot.window(xlim = lim(object, side), ylim = lim(object, side),  :
  nonfinite axis limits [GScale(-inf,0,2, .); log=1]
jordansread commented 7 years ago

does it work if you have data on those sides?

lindsayplatt commented 7 years ago

Oh I guess it eliminates the warnings, but it logs both axis instead of just one. It's also not assigning the correct side as logged.

gs <- gsplot() %>% 
    points(1:2, c(1,100), side=2) %>% 
    points(3:4, c(10,1000), side=4) %>% 
    axis(side=4) %>% 
    view(side=2, ylim=c(1,100)) %>% 
    view(side=4, ylim=c(1,100), log='y') 
gs

> logged(gs, side=1)
[1] FALSE
> logged(gs, side=2)
[1] TRUE
> logged(gs, side=4)
[1] FALSE

image

lindsayplatt commented 7 years ago

Reset par because that was causing an issue w/ always showing a logged axis when only one should be. Then we thought it had something to do with just view, but there are still logging issues when you don't use view.

Log side 4, but not side 2

[this works]

gs <- gsplot(xaxs='r', yaxs='r') %>% 
    points(1:3, c(1,10,100), side=2) %>% 
    points(3:5, c(10,100,1000), col="blue", side=4, log='y') %>% 
    axis(side=4)
gs

> logged(gs)
$side.1
[1] FALSE

$side.2
[1] FALSE

$side.4
[1] TRUE

image

Log side 2, but not side 4

[correct object, correct plot data, incorrect side 4 axis]

gs <- gsplot(xaxs='r', yaxs='r') %>% 
    points(1:3, c(1,10,100), side=2, log='y') %>% 
    points(3:5, c(10,100,1000), col="blue", side=4) %>% 
    axis(side=4)
gs

> logged(gs)
$side.1
[1] FALSE

$side.2
[1] TRUE

$side.4
[1] FALSE

image

lindsayplatt commented 7 years ago

This commit resets so that side 4 is not logged, but you lose the labels.

gs <- gsplot(xaxs='r', yaxs='r') %>% 
    points(1:3, c(1,10,100), side=2, log='y') %>% 
    points(3:5, c(10,100,1000), col="blue", side=4) %>% 
    axis(side=4)
gs

image

lindsayplatt commented 7 years ago

It's not something wrong with the y axes specifically, but with the second and third axes drawn. I changed the order the sides were drawn (2,4,1 instead), and it's the second and third sides drawn that are missing labels. So, this must mean it's something to do with our setting/re-setting par.

image

jordansread commented 7 years ago

or the tmp code in draw_axis? Have you looked at that?

lindsayplatt commented 7 years ago

Gah - keep forgetting about that tmp stuff. Going to search now

lindsayplatt commented 7 years ago

tmp isn't even used in the example above because there's only one call to each axis. I've compared par() right before the do.call('Axis', axis.args) line for when side 1 is called first and then when it's called second. No difference.

jordansread commented 7 years ago

so you checked to verify that the logic is correct for that tmp code and that it isn't evaluated anyhow?

lindsayplatt commented 7 years ago

I knew it didn't get evaluated because I put a breakpoint in there and it never stopped.

jordansread commented 7 years ago

OK, just want to rule it out completely - even if you put message('tmp used') in there, it doesn't hit that? Sometimes breakpoints are goofy in this package. Alternatively, debug(draw_axis)?

lindsayplatt commented 7 years ago

Correct, just tried that.

lindsayplatt commented 7 years ago

I reverted back trying out different commits (as far as #400) w/ the changes I made in this commit. Once I made the changes, the labels disappear so that issue is something that I introduced....now what about where I put par calls is causing that :thinking:

lindsayplatt commented 7 years ago

.......................still stumped.

lindsayplatt commented 7 years ago

First time I call the plot, neither y-axis shows up. Second time (as long as I don't clear it), they both show up, but the axis ticks & labels for side=4 are still wrong. Seems like there is something going on with all of the par(new=TRUE)

lindsayplatt commented 7 years ago

@jread-usgs to summarize what's going on here:

  1. When trying to log one axis independent of the axis of the same direction (1+3, or 2+4), both are logged. This is likely because of some par refresh that is not happening, so log='x' and log='y' propagates through to the next axis call. Using view to specify what to log has a similar effect. Setting log='' does not do anything either. Even though set_side_log looks like it should handle that appropriately.

These graphs are trying to have side=3 logged, but side=1 not logged.

# These two graphs label both side 1 and 3 as logged
# BUT they only log the side 3 data
gs <- gsplot() %>% 
  points(c(1,10,100), 1:3, side=1) %>% 
  points(c(10,100,1000), 3:5, col="blue", side=3, log='x') %>% 
  axis(side=3)
gs

gs <- gsplot() %>% 
  points(c(1,10,100), 1:3, side=1, log='') %>% 
  points(c(10,100,1000), 3:5, col="blue", side=3, log='x') %>% 
  axis(side=3)
gs

# both graphs say the correct logic behind-the-scenes
logged(gs, side=1)
[1] FALSE
logged(gs, side=3)
[1] TRUE

image

# These two graphs label both side 1 and 3 as logged
# AND log both side 1 and 3 data
gs <- gsplot() %>% 
  points(c(1,10,100), 1:3, side=1) %>% 
  points(c(10,100,1000), 3:5, col="blue", side=3) %>% 
  axis(side=3) %>% 
  view(side=3, log='x')
gs

gs <- gsplot() %>% 
  points(c(1,10,100), 1:3, side=1) %>% 
  points(c(10,100,1000), 3:5, col="blue", side=3) %>% 
  axis(side=3) %>% 
  view(side=1, log='') %>%
  view(side=3, log='x')
gs

# these graphs both say the exact opposite of what should be true:
logged(gs, side=1)
[1] TRUE
logged(gs, side=3)
[1] FALSE

image

  1. I implemented a change to resetting pars in this commit. This resulted in axes correctly being logged! BUT the labels were lost.
# Correctly logged data, but no labels for any side but 1
# AND side 1 labels are logged
gs <- gsplot() %>% 
    points(c(1,10,100), 1:3, side=1) %>% 
    points(c(10,100,1000), 3:5, col="blue", side=3, log='x') %>% 
    axis(side=3) %>% 
    axis(side=4)
gs

# checks out correctly again
logged(gs, side=1)
[1] FALSE
logged(gs, side=3)
[1] TRUE

image

  1. I just added this commit, which makes the plot where you log within the points call correct, but the logging using view is doing the exact opposite (but there are labels!).
gs <- gsplot() %>% 
  points(c(1,10,100), 1:3, side=1) %>% 
  points(c(10,100,1000), 3:5, col="blue", side=3, log='x') %>% 
  axis(side=3)
gs

gs <- gsplot() %>% 
  points(c(1,10,100), 1:3, side=1, log='') %>% 
  points(c(10,100,1000), 3:5, col="blue", side=3, log='x') %>% 
  axis(side=3)
gs

logged(gs, side=1)
[1] FALSE
logged(gs, side=3)
[1] TRUE

image

# side 1 data are logged, but side 3 data don't
# both axes are labeled as logged
gs <- gsplot() %>% 
    points(c(1,10,100), 1:3, side=1) %>% 
    points(c(10,100,1000), 3:5, col="blue", side=3) %>% 
    axis(side=3) %>% 
    view(side=3, log='x')
gs

# and that appears to be the behavior it expects
logged(gs, side=1)
[1] TRUE
logged(gs, side=3)
[1] FALSE

image

So in summary - with the changes to print.R that are currently on my axisLogWrongSide branch, it all seems to be working except for trying to log w/ view().

lindsayplatt commented 7 years ago

Ok, I believe I figured this out. The view.gsplot function takes side as an argument (with default c(1,2)) BUT was passing the arg side=c(1,2) into gather_function_info. So if side=3, it would always get passed in as c(1,2). See this commit for the change.

jordansread commented 7 years ago

Good find @lindsaycarr !

lindsayplatt commented 7 years ago

Fixed with #424 !!!!

It was those pesky pars - needed to reset only the pars that pertained to the side, but not all pars.