Too much noise on the x-axis GGPLOT

I have log data that I am trying to visualize. The logs contain 8 hours of data and I am trying to plot time on the x axis. But since the times are milliseconds apart I get a cluster on the X-axis.

The code I used:
df <- data %>%
group_by(location)

ggplot(df, aes(x = time, y = location)) + geom_point() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

How would I go about incrementing the time on x axis instead of plotting all the time points.

I suspect the column named time is being interpreted as text. If it were interpreted as a numeric time, ggplot would figure out reasonable tic spacing on its own. If you run

str(data)

what does it say about the column time?

It is a factor w/ 2708 levels

I am not sure whether your times span more than one day. In the example below, I assumed they are all on one day. I think the key to getting the plot to behave is to convert the times into a POSIXct Date Time value so ggplot will treat it as a number. I did this by pasting a date to the front of the time string and then using the function ymd_hms() from lubridate to convert that to a POSIXct. You would use the actual date of your data, of course. I also formatted the x axis to show only the time portion of the value, leaving out the day. Note the difference between the first plot, made before the conversion, and the second.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(dplyr)
library(ggplot2)
library(scales)
DF <- data.frame(Time = c("01:13:26", "03:47:10", "09:01:35", "12:33:42", 
                          "15:45:12", "18:01:22", "21:12:55", "23:14:39"),
                 Value = 1:8)
str(DF)
#> 'data.frame':    8 obs. of  2 variables:
#>  $ Time : Factor w/ 8 levels "01:13:26","03:47:10",..: 1 2 3 4 5 6 7 8
#>  $ Value: int  1 2 3 4 5 6 7 8
ggplot(DF, aes(Time, Value)) + geom_point()

DF <- DF %>% mutate(Time = paste("2019-11-12", Time), 
                    DateTime = ymd_hms(Time)) 
str(DF)
#> 'data.frame':    8 obs. of  3 variables:
#>  $ Time    : chr  "2019-11-12 01:13:26" "2019-11-12 03:47:10" "2019-11-12 09:01:35" "2019-11-12 12:33:42" ...
#>  $ Value   : int  1 2 3 4 5 6 7 8
#>  $ DateTime: POSIXct, format: "2019-11-12 01:13:26" "2019-11-12 03:47:10" ...

ggplot(DF, aes(DateTime, Value)) + geom_point() + 
  scale_x_datetime(labels = scales::time_format("%H:%M"))

Created on 2019-11-12 by the reprex package (v0.3.0.9000)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.