Note that there are some explanatory texts on larger screens.

plurals
  1. POTruncate data frame column characters and update legend based on result in R
    text
    copied!<p>I am writing an R script that incoorporates a data frame. The data frame has the following look:</p> <pre><code>mydf &lt;= 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 ... </code></pre> <p>I am using this code to plot the data:</p> <pre><code>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") </code></pre> <p>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.</p> <p>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. </p> <p>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.</p> <p><strong>One additional item:</strong> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload