I am writing an R script that incoorporates a data frame.
The data frame has the following look:
mydf <= read.csv('file', header = TRUE, sep=",")
mydf
....Prod Date AVG
189 CA123 2012/07/24 14:32:35 0.2424 0.22 0.25 0.27
190 JK489 2012/08/25 18:29:08 0.2402 0.22 0.25 0.27
191 CA15K 2012/07/24 13:49:07 0.2427 0.22 0.25 0.27
192 JA45A 2012/07/22 02:32:40 0.2455 0.22 0.25 0.27
193 JA3HS 2012/07/24 22:26:25 0.2410 0.22 0.25 0.27
194 CA429 2012/08/28 10:36:16 0.2351 0.22 0.25 0.27
195 JK345 2012/07/25 07:11:24 0.2419 0.22 0.25 0.27
...
I am using this code to plot the data:
plot(Date,mydf$AVG,xlab='Date',ylab='AVG',main='title')
legend("topright", legend = c(" "," "), text.width = strwidth("1,000,000"), lty = 1:2, xjust = 1, yjust = 1, title = "Prods")
The plot is working fine, but I am unable to get the Legend formatting down. What I want to do is place a legend in the top right that will display each Prod as a different color data point on the graph; however, Prod also needs to be trucated and only count as the first two characters in the column.
I know I can access all the variables by running: `mydf$Prod’, but is there a way to truncate each item in that frame reference to just two characters. I tried using round, but I am unable to perform any math operations, which makes sense.
Is there a way to truncate these variables and then paste them into the legend keeping the truncated format. The legend will need to be dynamic, because the Prod’s are constantly changing, and I run the script on different files.
One additional item: Ideally, I would like this to be done with just the standard librarys. Im not currently using ggplot, or any other graphing library, as the graphs I am creating are simple.
Try this: