Emman
Emman

Reputation: 4201

Graphics object is recognized as 'environment' type rather than 'list', thus not compatible with ggplot although it should be

I can't pass a graphics object to ggplot, and one symptom of the problem is that it's recognized as environment type, rather than list, but I'm not sure this is what causing the issue.

I use the gamlj package to generate a linear model of some data. The function gamljGLM returns a model and a plot, which is supposed to be compatible with ggplot functions. In other words, the plot generated by gamljGLM should return TRUE for is.ggplot(), but this isn't the case for me. Furthermore, when testing the plot object for its type, I get that typeof() returns environment, although in principle it should return list.

Steps for reproducing the issue

  1. Installing gamlj package (done successfully on R versions either 3.6.3 or 4.0.2)
## first, install 'devtools' package
install.packages("devtools")

## second, install 'gamlj'
devtools::install_github("gamlj/gamlj")
  1. Load some data
library(tidyverse)

day_1 <- rnorm(1000, mean = 77, sd = 18)
day_2 <- rnorm(1000, mean = 74, sd = 19)
day_3 <- rnorm(1000, mean = 80, sd = 5)
day_4 <- rnorm(1000, mean = 76, sd = 18)


df <- 
  cbind(day_1, day_2, day_3, day_4) %>%
  as_tibble() %>%
  gather(., key = day, value = mood, day_1:day_4) %>%
  mutate_at(vars(day), factor)

> df

## # A tibble: 4,000 x 2
##    day    mood
##    <fct> <dbl>
##  1 day_1  88.2
##  2 day_1  66.7
##  3 day_1  67.0
##  4 day_1  93.8
##  5 day_1  70.6
##  6 day_1  97.9
##  7 day_1  81.9
##  8 day_1  91.2
##  9 day_1  69.4
## 10 day_1  48.4
## # … with 3,990 more rows
  1. Model the data using gamlj::gamljGLM
p <- gamljGLM(df_as_df, 
              formula = formula("mood ~ day"), 
              plotError = "ci", 
              plotHAxis = "day")

> p

## GENERAL LINEAR MODEL

##  Model Info                                    
##  ───────────────────────────────────────────── 
##    Info                                        
##  ───────────────────────────────────────────── 
##    Estimate          Linear model fit by OLS   
##    Call              mood ~ 1 + day            
##    R-squared                      0.01558849   
##    Adj. R-squared                 0.01484944   
##  ───────────────────────────────────────────── 


##  MODEL RESULTS

##  ANOVA Omnibus tests                                                        
##  ────────────────────────────────────────────────────────────────────────── 
##                SS            df      F           p             η²p         
##  ────────────────────────────────────────────────────────────────────────── 
##    Model          16406.19       3    21.09267    < .0000001    0.0155885   
##    day            16406.19       3    21.09267    < .0000001    0.0155885   
##    Residuals    1036048.90    3996                                          
##    Total        1052455.08    3999                                          
##  ────────────────────────────────────────────────────────────────────────── 


##  Fixed Effects Parameter Estimates                                                                                                          
 ## ─────────────────────────────────────────────────────────────────────────────────────────────────────────## ───────────────────────────────── 
##    Names          Effect           Estimate      SE           Lower        Upper         β              ## df      t              p            
 ## ─────────────────────────────────────────────────────────────────────────────────────────────────────────## ───────────────────────────────── 
##    (Intercept)    (Intercept)      76.9554387    0.2545935    76.456293    77.4545841     0.00000000    ## 3996    302.2678295    < .0000001   
##    day1           day_2 - day_1    -2.1270930    0.7200993    -3.538889    -0.7152967    -0.13111742    ## 3996     -2.9538885     0.0031563   
##    day2           day_3 - day_1     3.3874303    0.7200993     1.975634     4.7992266     0.20880663    ## 3996      4.7041156     0.0000026   
##    day3           day_4 - day_1    -0.6973011    0.7200993    -2.109097     0.7144952    -0.04298275    ## 3996     -0.9683401     0.3329331   
 ## ─────────────────────────────────────────────────────────────────────────────────────────────────────────## ───────────────────────────────── 

Upon running p, along the model above, it automatically draws the plot too. In addition, the plot can be called directly by p$descPlot$plot.

p$descPlot$plot

  1. Passing the plot object to ggplot fails
p$descPlot$plot + xlab("blah_x_label")

Error in p$descPlot$plot + xlab("blah_x_label") : 
  non-numeric argument to binary operator

So I check this out:

> is.ggplot(p)

## [1] FALSE

And furthermore:

> typeof(p)

## [1] "environment"   ## in principle, this should be 'list'

To sum up

This plot should be of ggplot type, and a friend has been successful in applying ggplot functions onto p$descPlot$plot, because the function is designed to plot a ggplot object. But for me it fails. I'm aware that this is a specific case with a specific package. I've contacted the package maintainer, and even opened an issue on its github project, but so far haven't been responded. I'm posting this question here too since I hope that maybe this issue, although specific, lies on a more general principle of ggplot objects, which could be solved regardless of being familiar with this specific package/function. Any ideas?

Upvotes: 0

Views: 74

Answers (1)

mcfanda
mcfanda

Reputation: 46

In the new versions (>2.1.1) it should be solved

Upvotes: 1

Related Questions