I am trying to plot several line graphs from a list using a loop in R. The list temp.list
looks like this:
> temp.list[1]
$`1`
NEW_UPC Week1 Week2 Week3 Week4 Week5 Week6 Week7 Week8 Week9 Week10 Week11 Week12
5 11410008398 3 6 11 15 15 27 31 33 34 34 34 34
Life Status Num_markets Sales
5 197 1 50 186048.1
I use only some part of the data above to plot, specifically items 2 to 13 in the list will go on the y-axis, i.e. 3,6,11,15,...,34. For x-axis, I would like to have Week 1, Week 2, ..., Week 12 at the tick marks. As I don't know how to assign character values to x in gglplot command, I created a variable called weeks
for x-axis as below:
weeks = c(1,2,3,4,5,6,7,8,9,10,11,12)
The code that I used to generate the plot is below:
for (i in 1:2) {
markets= temp.list[[i]][2:13]
ggplot(data = NULL,aes(x=weeks,y=markets))+geom_line()+
scale_x_continuous(breaks = seq(0,12,1))+
scale_y_continuous(breaks = seq(0,50,5))
}
This code does not generate any plot. When I run just the below lines:
ggplot(data = NULL,aes(x=weeks,y=markets))+geom_line()+
scale_x_continuous(breaks = seq(0,12,1))+
scale_y_continuous(breaks = seq(0,50,5))
I get this error:
Error: geom_line requires the following missing aesthetics: y
In addition: Warning message:
In (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
row names were found from a short variable and have been discarded
Any help to fix this will be appreciated. I looked at some related discussions here, but I am not clear how to proceed.
Also, any better way to generate multiple plots is also welcome. From temp.list
, I am looking to generate over 300 separate line graphs (i.e, not all lines in one chart).