Jorge Paredes
Jorge Paredes

Reputation: 1088

Variable not found while using facet_wrap in r

this is my data:

> head(datos, 10)
   t_ventas        año     n
1    Grande 2012-03-28  2617
2   Mediana 2012-03-28  6281
3     Micro 2012-03-28 37641
4   Pequeña 2012-03-28 16676
5    Grande 2013-03-28  2833
6   Mediana 2013-03-28  6822
7     Micro 2013-03-28 40280
8   Pequeña 2013-03-28 17858

And I am trying to use this command ggplot to compute a graph

ggplot(datos) +
 aes(x = `Año`, y = n, fill = t_ventas, colour = t_ventas) +
 geom_line(size = 1L) +
 scale_fill_hue() +
 scale_color_hue() +
 labs(x = "3", y = "4", subtitle = "1", caption = "2") +
 theme_light() +
 theme(legend.position = "none") +
 facet_wrap(vars(t_ventas), scales = "free")

But I am getting this error:

Error in eval(variables_names, envir = envir, enclos = baseenv()) : 
  object 't_ventas' not found

I think it gotta do something with the facet_wrap function?

Thanks In Advance

Upvotes: 0

Views: 813

Answers (1)

Carey Caginalp
Carey Caginalp

Reputation: 432

As R is indicating, it can't find an object called "t_ventas." It is attempting to evaluate that as a literal variable name. Assuming you want to facet wrap around the name of the column in your dataset, all you should need to do is wrap it in quotes, i.e. facet_wrap("t_ventas"). If you want to have more than one column, you can put a character vector in its place.

Upvotes: 1

Related Questions