Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:52:30+00:00 2026-06-13T05:52:30+00:00

I am writing an R script that incoorporates a data frame. The data frame

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:52:31+00:00Added an answer on June 13, 2026 at 5:52 am

    Try this:

    mydf$Labels = substr(mydf$Prod, 1, 2)
    f = factor(mydf$Labels)
    l = levels(f)
    plot(mydf$Date, mydf$Avg, xlab="Date", ylab="Avg", col=f)
    legend("topright", legend = l, fill = 1:length(l), title = "Prods")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script that has two inputs: a file containing a list of
I'm writing a script that has to move some file around, but unfortunately it
I am writing a script that will look in a custom reports directory, copy
I'm writing a script that'll read through my ftpd logs and generate a hash
I am writing a script that is retrieving information from file places.sqlite (history) and
I am writing a python script that parses a 3D model file from one
I am writing a script that will look at a directory of Parent VHD
I'm writing a script that has to do something like this at one point:
I am writing a script that will transfer a file from one server to
I'm currently writing a script that exports data from a list of tables and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.