-1

Example database

For each item I want to plot the 5 different prices for both days on the same axis.

I can plot for just one item (i.e "a") but I want to produce multiple graphs and save them automatically.

My actual data set is much larger than the example.

I have been working with this code to try save the graphs, but it is doesn't quite work:

 mypath <- file.path("C:","R","SAVEHERE",paste("myplot_", names[i], ".jpg", 
sep = ""))

 jpg(file=mypath)
    mytitle = paste("my title is", names[i])
    plot(x,y, main = mytitle)
 dev.off()

EDIT: This the code I am using to produce 1 graph.

filter(name== "a")

w2=data.table(w2)
w3 = melt.data.table(data=w2, id.vars=c("Day", "Name"))

wplot = ggplot(w3, aes(x = variable, y = value)) + 
geom_point(aes(color=Day)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylim(-100, 300)

plot(wplot)

The graph looks like this. I want it to loop through all the names and save graphs that look like the second photo. I hope this information is more helpful than my initial post!

thank you

user167232
  • 11
  • 2
  • Please dont post images of code. post the actual data (or a small subset) by editing your question to include the output from dput(head(data)) – morgan121 Jan 30 '19 at 03:23
  • Please post the plot of one item and describe what *doesn't quite work right* mean. – Parfait Jan 30 '19 at 03:45

2 Answers2

0

Incredibly hard to help because you provide so little code but perhaps plot.new():

plot.new()
jpg(file=mypath)
mytitle = paste("my title is", names[i])
plot(x,y, main = mytitle)
dev.off()

If you make a reproducible example and also tell us what specifically isn't working we will be able to better help you.

AidanGawronski
  • 2,055
  • 1
  • 14
  • 24
0

Got it to work with this

    Names=levels(q$Name)

for(i in 1:length(Names)){

  mypath <- file.path("C:", "Users", "myname", "Documents", "Plots",paste("myplot_",
                                                                           Names[i], ".jpg", sep = ""))

  jpeg(file=mypath)

  q4 = filter(q3, q3$Name == Names[i])

  qplot = ggplot(q4, aes(x = variable, y = value)) + geom_point(aes(color=TableName)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylim(-100, 300) + 
    labs(title = Names[i], color = "Legend") +xlab("") + ylab("Price")

  plot(qplot)
  dev.off()
}

Based off this https://www.r-bloggers.com/automatically-save-your-plots-to-a-folder/

user167232
  • 11
  • 2