Marinaio
Marinaio

Reputation: 111

Simple plot in R

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

Upvotes: 0

Views: 90

Answers (2)

Dave2e
Dave2e

Reputation: 24139

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

Upvotes: 1

Jacky
Jacky

Reputation: 750

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/

Upvotes: 1

Related Questions