2

I have the time variable and other variables. I want to define my first variable as time and use ggplot to have a nice plot for the two other vaiables with regard to the time.

I didn't try any code because I am stuck on how to figure out what I want. However, here is an (Idea code) which might mimic what I want and before that, there is 15 row of my data:

data <- structure(list(ATime = structure(1:15, .Label = c("00:00", "00:05",
                                                   "00:10", "00:15", "00:20", "00:25", "00:30", "00:35", "00:40", 
                                                   "00:45", "00:50", "00:55", "01:00", "01:05", "01:10", "01:15", 
                                                   "01:20", "01:25", "01:30", "01:35", "01:40", "01:45", "01:50", 
                                                   "01:55", "02:00", "02:05", "02:10", "02:15", "02:20", "02:25"), class = "factor"), 
                                                      ASpeed5 = c(34, 40, 38, 55, 56, 60, 66, 49, 48, 29, 67, 78, 
                                                      39, 53, 73), BSpeed5 = c(23, 46, 63, 64, 72, 61, 49, 48, 
                                                      63, 68, 62, 27, 35, 45, 59)), row.names = c(NA, 15L), class = "data.frame")


data$ATime <- as.time(data$ATime)
ggplot(data, aes(x = ATime, y1 = ASpeed5, y2 = BSpeed5))+
  geom_line(color = y1, y2)

I am expecting two lines with different colors for each speed (y-axis) with regard to the time(x-axis)

mustafa
  • 203
  • 1
  • 8
  • 1
    Possible dupe: [convert character to time in R](https://stackoverflow.com/questions/12034424/convert-character-to-time-in-r) – markus Nov 01 '19 at 20:27

2 Answers2

3

This is a base R example but if you are frequently working with time I would recommend the lubridate package. In this example I convert the ATime variable into a Date class. You will want to set your own timezone (tz) if using real data but I defaulted to the standard here.

data$ATime <- as.POSIXct(as.character(data$ATime), format="%R", tz="UTC")

data %>% 
  ggplot(aes(x=ATime)) + 
  geom_line(aes(y=ASpeed5, col=1)) +
  geom_line(aes(y=BSpeed5, col=2)) +
  ylab("Speed") + xlab("Time")

enter image description here

Edit to show a subset of the data

To display a subset of data you could use a filter. In this case I am calling all data if time is 'less than' the 00:10:00 mark. Like all conditional statements, this it reads as greater than or less than but when the variable is in date format you can think of it as earlier or later than.

data %>% 
  filter(ATime <= "2019-11-01 00:10:00") %>% 
  ggplot(aes(x=ATime)) + 
  geom_line(aes(y=ASpeed5, col=1)) +
  geom_line(aes(y=BSpeed5, col=2)) +
  ylab("Speed") + xlab("Time")

NEW EDIT to show full 24 hour window with specified axis ticksenter image description here

Your comment sounds like you want to visualize the entire 24 hour period with set breaks. You didn't specify an interval so I choose 4 hours. This image is poorly scaled because data only occurs in the first hour. Be sure to use an appropriate scale for your visualizations.

data$ATime <- as.POSIXct(as.character(data$ATime), format="%R", tz="UTC")
fullday <- as.POSIXct(c("00:00", "24:00"), format="%R", tz="UTC")

data %>% 
  ggplot(aes(x=ATime)) + 
  geom_line(aes(y=ASpeed5, col=1)) +
  geom_line(aes(y=BSpeed5, col=2)) +
  scale_x_datetime(limits = fullday, breaks="4 hours", date_labels = "%R") +
  theme(axis.text.x = element_text(angle = 45, vjust=1, hjust=1)) +
  ylab("Speed") + xlab("Time")

enter image description here

Brian
  • 7,900
  • 1
  • 27
  • 41
jessefleri
  • 109
  • 5
  • thank you so much! that worked nicely. However, what if I want to have a plot for 24 hours. how can I compact the Time in a way that I can visualize just few parts of it?. I mean here adjusting the axes. I am sorry because I am new to the field so I do not know how to attach a picture or recommend your answer. – mustafa Nov 01 '19 at 22:40
  • If you want to plot just a subset of the data I would recommend using the `filter()` command from the `dplyr` package. It is integrated with the ggplot package that you are already using. See my edit above for what a filter would look like. To recommend an answer as being useful you can click to 'up' arrow next to my answer. To accept it as working for you, check mark the box. https://stackoverflow.com/help/someone-answers – jessefleri Nov 01 '19 at 23:01
  • @mustafa I'm hoping the edit above clarified your question. If it did please accept and close the question. – jessefleri Nov 06 '19 at 04:38
  • Thank you so much sir. Actually what I wanted is not filter. I want to depict the entire day (24 hours) with a tidy X-axis that shows specific times that I want to show only – mustafa Nov 06 '19 at 16:29
  • @mustafa See new edits to confirm if this version is more clear. – jessefleri Nov 06 '19 at 19:00
0

The hms package is useful for dealing with times

data %>% 
  mutate(ATime = as.character(ATime)) %>% # easier if character rather than factor
  mutate(xtime = hms::parse_hm(ATime)) %>% 
  gather(-ATime, -xtime, key = 'AB', value = 'Speed') %>% 
  ggplot(aes(x = xtime, y = Speed, colour = AB)) +
  geom_point() +
  geom_line() +
  scale_colour_hue(l=40) +. # make the default colours a little nicer
  labs(x = 'Time')

enter image description here

Tony Ladson
  • 3,539
  • 1
  • 23
  • 30