[英]How to mark the y-values of data points in a given ggplot object?
I'm trying to annotate the y-values of data points in a given ggplot object, after the fact.事后,我试图在给定的 ggplot object 中注释数据点的 y 值。
To have a reproducible example, I'll create a model on mtcars
data, using lm()
, and plot using sjPlot::plot_model()
.为了有一个可重现的示例,我将使用
lm()
在mtcars
数据上创建 model ,并使用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
.所以我的问题从这里开始:假设我得到了 object
given_p_object
。 I execute it and get the plot:我执行它并得到 plot:
> given_p_object
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()
)?是否可以在 plot 上标记每个点的 y 值,而不参考原始数据和导致 plot 的过程(因此忽略
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?换句话说,如何从当前的
given_p_object
中提取执行以下操作所需的信息?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.