0

I get the following error when I use grid.arrange:

Error: Aesthetics must be either length 1 or the same as the data (3): x, y

Please see a sample data below. I need for each value in column Security_Ticker create a graph where x = Period and y = Price and show all graphs in one panel with four graphs in a row.

df <- read.table(text = "Security_Ticker  Period  Price
                 aapl 201901  100
                 goog 201902  200
                 nvda 201901  150
                 aapl 201902  110
                 goog 201903  220
                 nvda 201902  155
                 aapl 201903  250
                 goog 201904  280", header =TRUE)

df.splitted= split(df,df$Security_Ticker)
plot_list = list()

for(i in 1:length(df.splitted)){
  p <- ggplot(data=df.splitted[[i]],aes(x=df.splitted[[i]][[2]], y=df.splitted[[i]][[3]])) + geom_line() + ggtitle(df.splitted[[i]][[1]][1]) 
  plot_list[[i]] = p
  print(p)
}

grid.arrange(plot_list,ncol=4)

I need to print all graphs from the plot_list in one panel with 4 graphs in a row, but I get the following error: Error: Aesthetics must be either length 1 or the same as the data (3): x, y

I know it should be easy to fix, but I have no experience with it.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • That's a problem with your plots, not with grid.arrange. See the marked dupe. – Gregor Thomas Oct 25 '19 at 16:13
  • I can plot them individually, but not in the plot list. – commandos1985 Oct 25 '19 at 16:14
  • `aes()` expects unquoted column names, the dupe goes through options for when you want to use variables for column names. – Gregor Thomas Oct 25 '19 at 16:14
  • I have no idea how to fix it. Can you please help me with it? – commandos1985 Oct 25 '19 at 16:16
  • I'm surprised they print in the loop, but if you call them afterwards they don't print. `plot_list[[1]]` gives the same error. I'm confident if you use the good practices in the dupe, it will work. – Gregor Thomas Oct 25 '19 at 16:19
  • Can you please provide me with a good link? I have spent already about three hours trying to fix this problem. Again, I have almost no experience with R and ggplot. – commandos1985 Oct 25 '19 at 16:24
  • The good link is at the top of your question in the yellow box. I would recommend following the top answer by David Robinson, using `aes_string()`. It's an older method, and easy to implement. https://stackoverflow.com/a/22309328/903061 – Gregor Thomas Oct 25 '19 at 16:25
  • Essentially you just change `aes(x=df.splitted[[i]][[2]], y=df.splitted[[i]][[3]])` to `aes_string(x = "Period", y= "Price")` – Gregor Thomas Oct 25 '19 at 16:27
  • Then, `grid.arrange` doesn't take a list argument, so you can use `do.call(gridExtra::grid.arrange, args = c(plot_list, ncol = 4))` to print the plots together. – Gregor Thomas Oct 25 '19 at 16:28
  • That said, you may want to try facets instead: `ggplot(df, aes(x = Period, y = Price)) +geom_line() + facet_wrap(~Security_Ticker, ncol = 4)` – Gregor Thomas Oct 25 '19 at 16:30
  • Thank you so much! I really appreciate your help! – commandos1985 Oct 25 '19 at 16:37
  • `grid.arrange` can take a list, you just have to pass it to the `grobs` argument i.e. `gridExtra::grid.arrange(grobs=plot_list, ncol = 4)` – user20650 Oct 25 '19 at 18:40

0 Answers0