1

I am trying to modify a graph produced by interaction library, which according to documentation is a ggplot object. Specifically, I want to modify the y axis limits.

library(interactions)

alpha = 5
w = 4
r = 4
wr = 4

w1 =  rnorm(1000,10,2)
r1 = rnorm(1000,10,2)
e = rnorm(1000,0,100)

y = alpha + w*w1 + r*r1 + wr*w1*r1 + e

m =  lm(y~ w1 + r1 + w1*r1)

p<- johnson_neyman(m,pred=r1,modx=w1,mod.range = c(0,100))

#element which is ggplot object
q <- p[3]

Based on search of past questions, I tried

q <- q +ylim(c(0,1000))

which gives "NULL".

Also tried

ggplot_build(q)

Which gives

Error in UseMethod("ggplot_build") : no applicable method for 'ggplot_build' applied to an object of class "list"

Martan
  • 565
  • 5
  • 17
  • 3
    Try `q <- p[[3]]` or `q <- p$plot`, i.e. use `[[` instead of `[`. – stefan Nov 26 '22 at 11:48
  • 1
    See e.g. [The difference between bracket \[ \] and double bracket [[ ]] for accessing the elements of a list or dataframe](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el) – stefan Nov 26 '22 at 11:51
  • 2
    @stefan already pointed out the suitable subsetting operator = double brackets . Here's some detail about the meaning of single vs. double brackets: https://adv-r.hadley.nz/subsetting.html#subset-single – I_O Nov 26 '22 at 11:53
  • 1
    You can do: `p$plot + ylim(c(0, 1000))` – Allan Cameron Nov 26 '22 at 16:05

1 Answers1

0

As pointed out in comments,

q <- p[[3]]

or

q <- p$plot

solved it.

Martan
  • 565
  • 5
  • 17