ggplot lost variable/column after chart

30 Views Asked by At

I'm using the geom_abline() after a geom_bar() with GGplot. I want to insert the abline with the mean of continuous data in y, but the error says that it is not able to find the variable."Error: object 'khwm2' not found"

enter image description here

If on intercept I put 120, it draws the line, but if I use mean(kwhm2) or mean(varm_tot/totyta) it says it can not find the variables.

Any idea?

Here is the code:

data_fbh %>% select(varm_tot, totyta, far1) %>%
  group_by(far1) %>%
  summarise(kwhm2 = mean(varm_tot/totyta) * 1000) %>%
  ggplot(aes(x=far1, y=kwhm2)) +
  geom_bar(stat="identity", fill="blue") +
  scale_x_discrete(labels = c("-1940","1941-1960","1961-1970",
                              "1971-1980","1981-1990","1991-2000",
                              "2001-2010","2011-2019")) +
  labs(x="Byggar", y="kWh/m2") +
  theme_light() +
geom_abline(slope = 0, intercept = mean(khwm2)) 
1

There are 1 best solutions below

1
Humberto R On

I just figured out, simple. I added the data frame reference inside abline.

geom_abline(slope = 0, intercept = mean(data_fbh$varm_tot/data_fbh$totyta)*1000)