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

Can we use gghighlight() for autoplot()? #66

Closed
hoxo-m opened this issue Sep 21, 2018 · 3 comments
Closed

Can we use gghighlight() for autoplot()? #66

hoxo-m opened this issue Sep 21, 2018 · 3 comments

Comments

@hoxo-m
Copy link

hoxo-m commented Sep 21, 2018

Hi, thank you for the greatest package.
Now, I'm trying to use gghighlight() for a result of autoplot(), but it occurs an error.

For instance:

library(glmnetUtils)
library(ggfortify)

model <- glmnet(medv ~ ., data = MASS::Boston)

g <- autoplot(model, xvar = "lambda")
g

rplot19

library(gghighlight)

g + gghighlight(variable == "crim", use_group_by = FALSE)

Error in mutate_impl(.data, dots) :
Evaluation error: object 'variable' not found

head(g$data)
#>  Df   Lambda  dev.ratio    index variable value
#> 1  0 6.777654 0.00000000 1.913631     crim     0
#> 2  1 6.175546 0.09238648 1.820597     crim     0
#> 3  2 5.626927 0.17376616 1.727563     crim     0
#> 4  2 5.127046 0.25269182 1.634530     crim     0
#> 5  2 4.671574 0.31820575 1.541496     crim     0
#> 6  2 4.256564 0.37259654 1.448462     crim     0

What would you suggest I do?

@yutannihilation
Copy link
Owner

yutannihilation commented Sep 21, 2018

Thanks, confirmed.

The reason of the error is that the data of the second layer doesn't contain variable.

library(glmnetUtils)
library(ggfortify)
#> Loading required package: ggplot2

model <- glmnet(medv ~ ., data = MASS::Boston)

g <- autoplot(model, xvar = "lambda")

lapply(g$layers, function(l) {
  if (is.data.frame(l$data)) {
    head(l$data)
  } else {
    l$data
  }
})
#> [[1]]
#> list()
#> attr(,"class")
#> [1] "waiver"
#> 
#> [[2]]
#>     crim zn indus     chas nox        rm age dis rad tax    ptratio
#> s21    0  0     0 0.128706   0 3.8961005   0   0   0   0 -0.6305235
#> s17    0  0     0 0.000000   0 3.5480395   0   0   0   0 -0.5076149
#> s14    0  0     0 0.000000   0 3.2365996   0   0   0   0 -0.3713991
#> s10    0  0     0 0.000000   0 2.6603223   0   0   0   0 -0.1192839
#> s7     0  0     0 0.000000   0 1.9756431   0   0   0   0  0.0000000
#> s3     0  0     0 0.000000   0 0.5694424   0   0   0   0  0.0000000
#>           black      lstat Df    Lambda dev.ratio       index  label_y
#> s21 0.002301817 -0.4975636  5 0.9607149 0.6653606 -0.04007759 4.272373
#> s17 0.000000000 -0.4828300  3 1.3938328 0.6418216  0.33205737 4.272373
#> s14 0.000000000 -0.4541851  3 1.8425650 0.6143107  0.61115860 4.272373
#> s10 0.000000000 -0.4011381  3 2.6732463 0.5432586  0.98329356 4.272373
#> s7  0.000000000 -0.3353772  2 3.5338742 0.4552422  1.26239478 4.272373
#> s3  0.000000000 -0.1969814  2 5.1270464 0.2526918  1.63452975 4.272373

Created on 2018-09-21 by the reprex package (v0.2.1)

Unfortunately, currently there's no way to apply gghighlight() only on the first layer due to this design decision: #12 (comment). (n = 1 can highlight only the second layer)

So, the workaround would be a bit tricky. Sorry for the inconvenience, I will consider implementing a new feature for this.

library(glmnetUtils)
library(ggfortify)
#> Loading required package: ggplot2

model <- glmnet(medv ~ ., data = MASS::Boston)

g <- autoplot(model, xvar = "lambda")

l <- g$layers[[2]]
g$layers[[2]] <- NULL

library(gghighlight)

g <- g +
  gghighlight(variable == "crim", use_group_by = FALSE)
#> label_key: variable

g$layers[[3]] <- l
g

Created on 2018-09-21 by the reprex package (v0.2.1)

@hoxo-m
Copy link
Author

hoxo-m commented Sep 23, 2018

Thank you!
It made me understood that the cause is not in autoplot.
And the workaround was well done for me.

@yutannihilation
Copy link
Owner

FYI: This is now possible with the dev version.

library(gghighlight)
#> Loading required package: ggplot2
library(glmnetUtils)
library(ggfortify)

model <- glmnet(medv ~ ., data = MASS::Boston)

autoplot(model, xvar = "lambda") +
  gghighlight(variable == "crim", use_group_by = FALSE)
#> Warning: Could not calculate the predicate for layer 2; ignored
#> label_key: variable

Created on 2018-12-28 by the reprex package (v0.2.1)

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

No branches or pull requests

2 participants