1

I'm trying to annotate the y-values of data points in a given ggplot object, after the fact.

To have a reproducible example, I'll create a model on mtcars data, using lm(), and plot using sjPlot::plot_model().

library(magrittr)
library(sjPlot)

given_p_object <- 
  mtcars %>% 
  lm(mpg ~ as.factor(gear), data = .) %>% 
  sjPlot::plot_model(., type = "pred")

So my question starts here: say that I'm given the object given_p_object. I execute it and get the plot:

> given_p_object

given_plot

Is it possible to mark the y-values for each point on the plot, without referring back to the original data and the process that led to the plot (thus ignoring mtcars %>% lm() %>% sjPlot::plot_model())? In other words, how can I extract from within the current given_p_object the information needed to do the following? demo

Emman
  • 3,695
  • 2
  • 20
  • 44

1 Answers1

1

Those values can be found in :

given_p_object$gear$data$predicted
#[1] 16.1 24.5 21.4

A general solution would be :

get_predicted_value <- function(p) p[[1]]$data$predicted
get_predicted_value(given_p_object)
#[1] 16.1 24.5 21.4
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you, that's useful. Can I generalize this, such that for any given ggplot object `p`, I could get the y-values for data points? I'm asking because here we need to specify `gear`. I'm trying to be completely ignorant about the plot when pulling out the y-values. – Emman Jan 15 '21 at 10:57
  • Updated the answer to include a function to work with any `p` object. – Ronak Shah Jan 15 '21 at 10:59
  • Sorry if this gets annoying, but from this I'm understanding that there isn't a universal way to get the y-values? Consider, for example, `g <- ggplot(mtcars, aes(wt, mpg)) + geom_point()`. Then `get_predicted_value(g)` returns `NULL`. – Emman Jan 15 '21 at 11:13
  • Hmmm...Sorry I am not aware of a general solution where it can return y-values from any plot. Would you expect the y-values to be returned from this plot as well? `plot(1:10, 11:20)` – Ronak Shah Jan 15 '21 at 11:19
  • No, because it's not a ggplot object. `blah <- plot(1:10, 11:20)` `is.ggplot(blah)` returns `FALSE`. – Emman Jan 15 '21 at 11:23