Home > Enterprise >  data column not recognized in the ggplot geom_hline
data column not recognized in the ggplot geom_hline

Time:09-24

I was wondering why variable mean_y is not recognized by my geom_hline(yintercept = unique(mean_y)) call?

library(tidyverse)

set.seed(20)
n_groups <- 2
n_in_group <- 20
sd_e = 2
groups <- gl(n_groups, n_in_group, labels = c("T","C"))
age <-rnorm(length(groups), 25, 3)
betas <- c(5,0,0,2)
dat <- data.frame(groups=groups,age=age)

X <- model.matrix(~ groups * age, data = dat)

lin_pred <- as.vector(X %*% betas)

dat$y <- rnorm(nrow(X), lin_pred, sd_e)


dat %>% group_by(groups) %>%  mutate(mean_y = mean(y)) %>%
  ungroup() %>%
ggplot() aes(x = age, y = y)  
  geom_point(aes(color=groups))  
  geom_hline(yintercept = unique(mean_y)) # Error in unique(mean_y) :
                                          # object 'mean_y' not found

CodePudding user response:

Variables need to be inside aes(), try:

geom_hline(aes(yintercept = mean_y))
  • Related