0

I am an R novice.

I have been able to get my header-less data into columns:

my_data<- separate(my_data,col = "V1",into = c("Date", "Tool","Dept","Port","Host","Min","Max"),sep = ":")

It looks like this:

          Date   Tool         Dept  Port     Host Min Max
1: 03-Mar-2019 toolset Headquaters  1234 host.com   1   7
2: 10-Mar-2019 toolset Headquaters  1234 host.com   0   7
3: 17-Mar-2019 toolset Headquaters  1234 host.com   1   7

I plot it:

> p1 <- ggplot() + geom_line(aes(y = Max, x = Date),data = My_data)
> p1

But all I get is this:

enter image description here

How can I plot the min/max over time?

EDIT1: These are dates, not factors or anything else

EDIT2: I tried the suggestion:

my_data$Date <- as.Date(lmt$Date, "%d-%b-%Y")

and got newest

Marinaio
  • 111
  • 8
  • Why don't you add the data argument to `ggplot`? – NelsonGon Apr 17 '19 at 17:02
  • Is it not ...,data = my_data) ? that is my data frame. – Marinaio Apr 17 '19 at 17:04
  • What error if any do you get as your sample data is not enough to make a plot? – NelsonGon Apr 17 '19 at 17:05
  • No error. The data is hundreds of lines so I am experimenting with something small. The plot is the problem. It does not show what the max and the min was per day. – Marinaio Apr 17 '19 at 17:07
  • Basically all you need to do is get your data into long form - convert the `min` and `max` columns into one colum, then plot by that column. It is better to specify the data in the `ggplot` call rather than the subsequent calls to `geoms` unless necessary. – bob1 Apr 17 '19 at 17:10
  • You write `data = My_data` and not `data = my_data`? – Anders Ellern Bilgrau Apr 17 '19 at 17:24
  • 1
    Those are not dates, they are strings (or factors). Just do `My_data$Date <- as.Date(My_data$Date, "%d-%b-%Y")`. And it worked with me. – Rui Barradas Apr 17 '19 at 17:38
  • As mentioned, I am a novice here: is My_data$Date <- as.Date(My_data$Date, "%d-%b-%Y") supposed to convert just the column named "date" to a date? ... and keep the rest of the data? – Marinaio Apr 17 '19 at 18:08
  • I tried it an it appears to work, right? So now am I looking at a true date? – Marinaio Apr 17 '19 at 18:20
  • How can I also show what was the min for that day? – Marinaio Apr 17 '19 at 18:20
  • 1
    It seems there are data type issues, which are hard to help with without having a sample of *actual* data, such as from `dput`. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on reproducible R posts – camille Apr 17 '19 at 18:30
  • Here is my raw data:03-Mar-2019:toolset:Headquaters:1234:host.com:1:7 10-Mar-2019:toolset:Headquaters:1234:host.com:0:7 17-Mar-2019:toolset:Headquaters:1234:host.com:1:7 – Marinaio Apr 17 '19 at 18:45
  • > dput(head(my_data,4)) structure(list(Date = structure(c(17958, 17965, 17972), class = "Date"), Tool = c("toolset", "toolset", "toolset"), Dept = c("Headquaters", "Headquaters", "Headquaters"), Port = c("1234", "1234", "1234"), Host = c("host.com", "host.com", "host.com"), Min = c("1", "0", "1"), Max = c("7", "7", "7")), class = c("data.table", "data.frame"), row.names = c(NA, -3L), .internal.selfref = ) – Marinaio Apr 17 '19 at 18:54

2 Answers2

1

There may be a problem with how your datetime is structured at the moment. you can run an str(my_data) to see how your date is formatted. There are many date formats but POSIXct is the best imo. If the date is a factor or anything else, convert it to a character by as.character()

After it's converted, you can convert your datetime with strptime(my_data$Date, %d-%b-%Y) Once the date is formatted properly, you can run your ggplot:
p1 <- ggplot(my_data, aes(x = Date, y = Max)) + geom_line()

I have full details in my tutorial below to help you out in case you need it. Dates can be a little tricky to work with, especially when you have to specify the format argument in strptime() ex(%M or %d). There's a chart on the site that lists all the possible formats.

https://jackylam.io/tutorial/uber-data/

Jacky
  • 710
  • 2
  • 8
  • 27
  • 2
    How is this an answer? Have you tried that code line? In your tutorial there are clues as how to solve the OP's problem, yes, but you should post an answer, not just a link. Link only answers are a reason to flag as VLQ. – Rui Barradas Apr 17 '19 at 17:39
  • 1
    you're right. I'll edit my answer to include edits to datetime. – Jacky Apr 17 '19 at 17:43
  • Is thie what you were asking? $ labels :List of 2 ..$ x: chr "Date" ..$ y: chr "Max" – Marinaio Apr 17 '19 at 18:05
  • your data is in a list? how are you importing your data? – Jacky Apr 17 '19 at 18:12
  • @Jacky I have a text file and this is how Ithought I could import: my_data <-read.table(file="my_data_file.txt",nrows=3, header=F) – Marinaio Apr 17 '19 at 18:38
1

Here is your basic plot:

My_data<-read.table(header=TRUE,, text="Date   Tool         Dept  Port     Host Min Max
03-Mar-2019 toolset Headquaters  1234 host.com   1   7
10-Mar-2019 toolset Headquaters  1234 host.com   0   7
17-Mar-2019 toolset Headquaters  1234 host.com   1   7")

My_data$Date <- as.Date(My_data$Date, "%d-%b-%Y")

library(ggplot2)
p1 <- ggplot(data=My_data, aes(x=Date)) + 
  geom_line(aes(y = Max), col="blue") +
  geom_line(aes(y = Min), col="green")
print(p1)

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • I'm going to mark this as the answer. It is close to what I had imagined. Now I need to find out how to load the real data file full of raw data and automate the process. thanks – Marinaio Apr 18 '19 at 19:54