Working with time stamp scale is different from let say a continuous or discrete scale.
The data source link.
Lets look at the basic plot
# Load libraries
library(ggplot2)
# The set your working directory
setwd("C:/R_Train") # please replace with your own directory
# read the data into a dataframe
tbl <- read.csv("Sample Data/temp.csv")
# convert time stamp
tbl$Time_Stamp <- as.POSIXct(tbl$Time_Stamp, format="%m/%d/%Y %H:%M")
# basic plot
ggplot(tbl, aes(x=Time_Stamp, y=Area)) +
geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
theme(panel.grid.major.x = element_line(color="chocolate4"),
panel.grid.minor.x = element_line(color = "cyan4")) +
theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
theme(axis.text.x = element_text(size = 15))+
theme(axis.text.y = element_text(size = 15)) +
theme(plot.margin = unit(c(0, 0, 20, 20), "pt"))
Now let see how to set the x scale. Let say major axis every hour.
Code
ggplot(tbl, aes(x=Time_Stamp, y=Area)) +
geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
theme(panel.grid.major.x = element_line(color="chocolate4"),
panel.grid.minor.x = element_line(color = "cyan4")) +
theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
theme(axis.text.x = element_text(size = 15))+
theme(axis.text.y = element_text(size = 15)) +
theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
scale_x_datetime(breaks = "hour")
Output
As you can see the overlapping of x axis text (label). We can fix it by changing angle.
Code
ggplot(tbl, aes(x=Time_Stamp, y=Area)) +
geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
theme(panel.grid.major.x = element_line(color="chocolate4"),
panel.grid.minor.x = element_line(color = "cyan4")) +
theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
theme(axis.text.x = element_text(size = 15))+
theme(axis.text.y = element_text(size = 15)) +
theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
scale_x_datetime(breaks = "hour") +
theme(axis.text.x = element_text(size = 6,angle = 90))
Output
It is possible to change the scale in different scales, for example every 4 hours.
Code
ggplot(tbl, aes(x=Time_Stamp, y=Area)) +
geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
theme(panel.grid.major.x = element_line(color="chocolate4"),
panel.grid.minor.x = element_line(color = "cyan4")) +
theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
theme(axis.text.x = element_text(size = 15))+
theme(axis.text.y = element_text(size = 15)) +
theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
scale_x_datetime(breaks = "4 hour") +
theme(axis.text.x = element_text(size = 10,angle = 90))
Output
But if you still need the hourly x scale. You can do with minor breaks.
Code
ggplot(tbl, aes(x=Time_Stamp, y=Area)) +
geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
theme(panel.grid.major.x = element_line(color="chocolate4"),
panel.grid.minor.x = element_line(color = "cyan4")) +
theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
theme(axis.text.x = element_text(size = 15))+
theme(axis.text.y = element_text(size = 15)) +
theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
scale_x_datetime(breaks = "4 hour", minor_breaks = "1 hours") +
theme(axis.text.x = element_text(size = 10,angle = 90))
That is the end of Part V of Plotting In R.
If you have any specific request configuring ggplot2, please leave a comment.
I will try to add to in the current posts or cover that in future posts.