简体   繁体   English

如何标记给定ggplot object中数据点的y值?

[英]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

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() )?是否可以在 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.

相关问题 如何在 ggplot 中绘制 y 值向量? - How can I plot a vector of y-values in ggplot? Scatterpie:如何在连接饼图的线上添加注释以标记饼图之间 y 值的百分比变化 - Scatterpie: How to add annotation over line connecting pies to mark percent change in y-values between pies ggplot + geom_bar的y值显示不正确 - y-values of ggplot+geom_bar not showing properly 如何在 plot 线上添加注释以标记离散 x 值之间 y 值的百分比变化 - How to add annotation over line plot to mark percent change in y-values between discrete x-values 根据[r]中的列对数据重新排序,以将ggplot中的x值项从最低y值排序到最高y值 - Reordering data based on a column in [r] to order x-value items from lowest to highest y-values in ggplot 创建Y值的功能 - Function to Create Y-Values ggplot2 - 在条形图中以正负 y 值倒序堆叠的有序因子 - ggplot2 - ordered factor stacking in inverted order in barplot with positive and negative y-values 使用 ggplot2 在 x 轴上创建带有虚拟变量的条形图,并为条形图提供 y 值 - Creating a bar chart with ggplot2 with dummy variables on x-axis as well as providing the y-values for the barplot plotrix:如何在没有y值的情况下绘制CI。 y值超出置信区间 - plotrix: How to plotCI without the y-values. y-values are outside the confidence interval 计算改变 y 值的斜率 - Calculating the slope with changing y-values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM